Example how to use cURL functions and to send http (or https) cURL request

Posted by nikola, With 0 Comments, Category: PHP, Tags: ,

Using cURL functions, you'll be able to connect and communicate to many different types of servers with many different types of protocols - http, https, ftp, gopher, telnet, dict, file, and ldap protocols.

Next example sends http (or https) cURL request, with variables like $username, $password, $recipient and $body

$url = 'https://www.desitnationurl.com';
 $fields = array(
 'username'=>urlencode( $username ),
 'password'=>urlencode( $password ),
 'recipient'=>( $recipient ),
 'body'=>urlencode( $body )
 );
//url-ify the data for the POST
 $fields_string = '';
 foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
 rtrim($fields_string,'&');
// magic begins here
 //open connection
 $ch = curl_init();
//set the url, number of POST vars, POST data
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_POST,count($fields));
 curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
 $result = curl_exec($ch);
//close connection
 curl_close($ch);
 // and that's all

Give a try, I hope it works as you expected.