How to switch from POST to GET in PHP CURL

I tried switching from a previous request to a Get request. Which implies its Get, but ultimately makes the message.

I tried the following in PHP:

curl_setopt($curl_handle, CURLOPT_POSTFIELDS, null); curl_setopt($curl_handle, CURLOPT_POST, FALSE); curl_setopt($curl_handle, CURLOPT_HTTPGET, TRUE); 

What am I missing?

Additional Information: I already have a connection to configure the POST request. This succeeds, but later, when I try to reuse the connection and switch back to GET using setopts above, it still finishes doing POST internally with incomplete POST headers. The problem is that he believes that he is executing a GET, but ends up setting the POST header without the content length parameter, and the connection fails with 411 ERROR.

+59
post php curl get
Aug 04 '09 at 1:50
source share
5 answers

Solved: problem here:

I set POST through _CUSTOMREQUEST and _POST , and _CUSTOMREQUEST saved as POST , and _POST on _HTTPGET . The server assumed that the header from _CUSTOMREQUEST was correct and returned from 411.

 curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST'); 
+28
Aug 04 '09 at 18:56
source share

Make sure you send the query string at the end of the URL when executing the GET request.

 $ qry_str = "? x = 10 & y = 20";
 $ ch = curl_init ();

 // Set query data here with the URL
 curl_setopt ($ ch, CURLOPT_URL, 'http://example.com/test.php'. $ qry_str); 

 curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt ($ ch, CURLOPT_TIMEOUT, 3);
 $ content = trim (curl_exec ($ ch));
 curl_close ($ ch);
 print $ content;
 With a POST you pass the data via the CURLOPT_POSTFIELDS option instead 
 of passing it in the CURLOPT__URL.
 -------------------------------------------------- -----------------------

 $ qry_str = "x = 10 & y = 20";
 curl_setopt ($ ch, CURLOPT_URL, 'http://example.com/test.php');  
 curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt ($ ch, CURLOPT_TIMEOUT, 3);

 // Set request method to POST
 curl_setopt ($ ch, CURLOPT_POST, 1);

 // Set query data here with CURLOPT_POSTFIELDS
 curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ qry_str);

 $ content = trim (curl_exec ($ ch));
 curl_close ($ ch);
 print $ content;

Note curl_setopt() docs for CURLOPT_HTTPGET (highlighted by me):

[Set CURLOPT_HTTPGET equal] TRUE to reset the HTTP request method for GET.
Since GET is the default, this is only necessary if the request method has been changed.

+84
Aug 4 '09 at 2:43
source share

Add this before calling curl_exec ($ curl_handle)

 curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET'); 
+38
May 04 '12 at 6:20
source share

The default CURL request is GET, you do not need to set any parameters for the GET CURL request.

+2
Jan 17 '17 at 0:36
source share

Here is a pretty decent example of the difference:

http://www.weberdev.com/get_example-4606.html

-3
Aug 04 '09 at 2:09
source share



All Articles