PHP : HTTP Post with Curl

A sample function to use curl post data.

$data=array()

$data=array();

$data[“field1″]=”hello world”;

$data[“user”]=”hello user”;

curl_post($url,$data)

function curl_post($url,$fields) { //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)’); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields); //some other users translate this array to string, but it will require you to convert some special chars in url such as &

//execute post $result = curl_exec($ch); //close connection curl_close($ch); return $result; }

Leave a Reply

Your email address will not be published. Required fields are marked *