Showing posts with label http request. Show all posts
Showing posts with label http request. Show all posts

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).

AJAX Cross Domain

Virtually all modern server-side scripting languages support remote requests in one or another form. In client-side javascript, this functionality is not present due to the Same Origin Policy. Though this policy is necessary for a robust security model, programmers are often handicapped in their wish to send and retrieve requests to remote servers. During the last years, several proposals have been made for a more flexible (re-)design of the XMLHttpRequest object, which is the core mechanism of all AJAX based technologies. AJAX Cross Domain provides in the full functionality of the XMLHttpRequest object in a similar syntax, and can by extent handle all cross-domain requests. The core engine of AJAX Cross Domain is written in Perl and outputs its content as a javascript file.

Check out this URL for the solution: http://www.ajax-cross-domain.com/