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.
You should also read:
- PHP cURL: Fetching URL and Sending Request with Cookies
- Pretend your scraper script as a browser when scraping in PHP
- How to pass variable values in URL from page to page with PHP?
- PHP: How to distinguish values in $_POST or $_GET that are sent via HTTP requests and those that are set / assigned in the code
- JavaScript: Confirmation / Warning before Leaving or Navigating Away from a Page


Facebook
Twitter
Google Plus
{ 4 comments… read them below or add one }
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 :)