CURL, a simple way to send HTTP Requests

Definition: CURL


curl is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks.


Ref: http://curl.haxx.se/


Using CURL in XAMPP


There appear to be a lot of misguided people on the intarwebs claiming all sorts of varying things you have to do to get CURL to work on a Windows-based XAMPP install. I’d like to clear them all up here and now.


It’s really quite simple - uncomment extension=php_curl.dll in your php.ini file available in the Apache\bin directory, then restart Apache


Ref: http://incoherentbabble.com/2007/04/21/using-curl-in-xampp/


Example



//initialize the request variable
$request = "";

//this is the username of our TM4B account
$param["username"] = "abcdef";

//this is the password of our TM4B account
$param["password"] = "12345";

//this is the message that we want to send
$param["msg"] = "This is sample message.";

//these are the recipients of the message
$param["to"] = "447768254545|447956219273|447771514662";

//this is our sender
$param["from"] = "MyCompany";

//we want to send the message via first class
$param["route"] = "frst";

//we are only simulating a broadcast
$param["sim"] = "yes";

//traverse through each member of the param array
foreach($param as $key=>$val){
//we have to urlencode the values
$request.= $key."=".urlencode($val);
//append the ampersand (&)
//sign after each paramter/value pair
$request.= "&";
}
//remove the final ampersand sign from the request
$request = substr($request, 0, strlen($request)-1);

//this is the url of the gateway's interface
$url = "http://www.tm4b.com/client/api/send.php";

//initialize curl handle
$ch = curl_init($url);

//set the url
curl_setopt($ch, CURLOPT_URL, $url);

//return as a variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

//set POST method
curl_setopt($ch, CURLOPT_POST, 1);

//set the POST variables
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

//run the whole process and return the response
$response = curl_exec($ch);

curl_close($ch); //close the curl handle
//show the result onscreen for debugging
//print $response;

Ref: http://www.sephiroth.it/tutorials/flashPHP/sms/

(in the above code from this link modified for error free result. The bolded parameter in the code was missed unexpectedly).

No comments: