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.

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

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


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

  3. Pingback: PHP: What would be the best approach to a script which submits data to different URLs? (eg 1 page to submit to multiple free classified websites). Current preference is PHP using cURL or Python. - Quora

Comments are closed.

Scroll to Top