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.
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
help me please !!!!!!!!!!!11
”
$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);
“
Very useful tutorial
thanks 🙂
[…] which to post the form, and execute your cURL for each of themHow to make a POST request with cURL: https://www.kavoir.com/2009/04/ph…Embed QuoteComment Loading… • Post • Embed • Just now Add […]