Does php curl issue an http GET request or an POST request?

php curl is often used to fetch remote content. Sure you can use file_get_content to fetch remote web pages. But curl has more options to choose. We know that http defines multiple methods to fetch a url such as GET, POST, etc. Do you know which method php curl uses to fetch the content of a url? By default, curl uses GET:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://myprogrammingnotes.com");
curl_setopt($curl, CURLOPT_HEADER,1);
curl_setopt($curl, CURLOPT_FRESH_CONNECT,1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false); 
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
try
{
  $results = curl_exec($curl);
  $info = curl_getinfo($curl);
  if (FALSE === $results)
    throw new Exception(curl_error($curl), curl_errno($curl));
}
catch(Exception $e) {

    trigger_error(sprintf(
  'Curl failed with error #%d: %s',
  $e->getCode(), $e->getMessage()),
  E_USER_ERROR);

}

curl_close($curl);

The first line of the http request curl issues is:

GET / HTTP/1.0

If you want to use POST instead of GET, you should add the following line:

curl_setopt($curl, CURLOPT_POST, TRUE);

Now the first line of the http header of the request would be:

POST / HTTP/1.0

But you do not really need to explicitly set CURLOPT_POST to true to use POST. If you set the CURLOPT_POSTFIELDS option, curl will issue the POST request even CURLOPT_POST is not set or set to false.

Reference:https://stackoverflow.com/questions/1225409/how-to-switch-from-post-to-get-in-php-curl

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Comments are closed, but trackbacks and pingbacks are open.