首页/Home PHP Common Programming Making POSTs With Curl

Making POSTs With Curl

PrintE-mail
Sunday, 10 February 2008 01:00  

Curl is powerful. It allows you to connect and communicate to many different types of servers with many different types of protocols. It can do more than what fsockopen() can.

For how to enable curl, check a related article called " A Short Example To Show How To Use Curl" on this site.

Here is a short example for posting some message to a remote url.

<?php
$str
= "a=b&c=d";
$url = "http://www.website.com/samplepage";

// Set up cURL connection
$ch = curl_init();

// Apply some settings
// For more info about this, have a look at http://php.net
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);

$data = curl_exec($ch);
curl_close ($ch);

if (!
$data)
{
die(
sprintf('Error [%d]: %s', curl_errno($ch), curl_error($ch)));
}
else
{
echo
$data;
}
?>