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
Comments are closed, but trackbacks and pingbacks are open.