Categories
HTTP Tips & Tutorials PHP Tips & Tutorials

PHP cURL: Making POST Request to URL

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.

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.

5 replies on “PHP cURL: Making POST Request to URL”

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


$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);

Comments are closed.