php Tutorials
Creating An RSS Feed With PHP And MySQL
Retrieving XML With Curl and SimpleXML
Using curl to Query Remote Servers
MySQL Basics
PHP mySQL Voting Booth
File Download Security
PHP Fundamentals

Using curl to Query Remote Servers

Curl and form data

Okay now that we can pull static pages from remote servers lets move into posting information into web forms automatically. The default method of sending form data with curl is GET. In the following example we'll send a text message to our cell phone via the web form. Our example web form will require data in the POST format and contains the fields pNUMBER , MESSAGE, SUBMIT.

<? 
$phoneNumber = '4045551111';
$message = 'This message was generated by curl and php';
$curlPost = 'pNUMBER='  . urlencode($phoneNumber) . '&MESSAGE=' . urlencode($message) . '&SUBMIT=Send';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/sendSMS.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec();
curl_close($ch);
 ?>

Here we set the phone number and message for the form into variables. The $curlPost variable is being used to store the POST data curl will use. When forming the $curlPost variable which will be used by curl_setopt later be sure to urlencode your data prior to passing it to curl_setopt. CURLOPT_POST is the option used to tell curl to send the form response via the POST method. CURLOPT_POSTFIELDS is the curl option used to store the POST data.

Curl Introduction <<  1 2 3 4 5  >> Curl and proxies
New Content