PHP cURL: Making POST Request to URL

by Yang Yang on April 22, 2009

Share This Article:
Subscribe to Kavoir: blog feed

By default, all HTTP requests made out by cURL is GET requests that simply fetches the URL and don’t submit any more POST variables.

However, if you need to fetch and retrieve URL by the POST method with cURL, you need the snippet below:

$url = 'http://www.example.com/submit.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);

Make sure you have set CURLOPT_POST to true and attached the POST variables in the form of a GET series of name=value couples by CURLOPT_POSTFIELDS.

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

{ 4 comments… read them below or add one }

thep nguyen July 17, 2010 at 2:47 am

why i can’t request with “post” from url. i coppy and past your code but it isn’t sucssecful

Reply

thep nguyen July 17, 2010 at 2:48 am

help me please !!!!!!!!!!!11

Reply

thep nguyen July 17, 2010 at 2:49 am


$soMay= ’3880105′;
$maTinh= ’059′;
function post_cn($soMay,$maTinh){
$url = ‘http://danhba.vdc.com.vn/tracuu/danhba/search.asp';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = ‘SoMay=’.$soMay.’&MaTinh=’.$maTinh;
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
var_dump($page);
}
post_cn($soMay,$maTinh);

Reply

Differine April 10, 2011 at 10:13 am

Very useful tutorial
thanks :)

Reply

Leave a Comment

Previous post:

Next post: