CodeIgniter sends POST data to specific URL API data

I need to send data to a URL, I did this before using the GET method and using url_encode()

Now that I need POST with a CodeIgniter framework, I don’t know very well what to use

Any help would be appreciated

+6
source share
1 answer

Ok, sending a response to avoid a time delay to find a solution to the future

Purpose: sending POST data to a specific URL, namely in the API

Frame: Codeigniter

Decision:

Suppose that the required data to transfer is an array with a name, email address, password

 $passData = array( "name" => $_name, "email" => $_email, "password" => $_pass ); $url = '192.168.100.51/AndroidApi/v2/register'; echo '<br><hr><h2>'.$this->postCURL($url, $params).'</h2><br><hr><br>'; 

The function of sending data via POST to the URL: yes we will use cURL here

 public function postCURL($_url, $_param){ $postData = ''; //create name value pairs seperated by & foreach($_param as $k => $v) { $postData .= $k . '='.$v.'&'; } rtrim($postData, '&'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_POST, count($postData)); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); $output=curl_exec($ch); curl_close($ch); return $output; } 

To check if cURL is available for use

 echo (is_callable('curl_init')) ? '<h1>Enabled</h1>' : '<h1>Not enabled</h1>' ; 

Thanks, and it works great with output as

Renouncement:

{"error": true, "message": "Email address is invalid"}

OR Success:

{"error": false, "message": "You have successfully registered"}

+7
source

Source: https://habr.com/ru/post/989910/


All Articles