How to switch from POST to GET in PHP CURL

来源:互联网 发布:java中断文件上传原理 编辑:程序博客网 时间:2024/04/30 20:51


7down votefavorite
3

I have tried switching from a previous Post request to a Get request. Which assumes its a Get but eventually does a post.

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 that's setup to do a POST request. That completes successfully but later on when I try to reuse the connection and switch back to GET using the setopts above it still ends up doing a POST internally with incomplete POST headers. The problem is it believes its doing a GET but ends up putting a POST header without the content-length parameter and the connection fails witha 411 ERROR.

share|edit
 
1 
Be careful with Get requests. Google has fun with them. ;) – Chris Lively Aug 4 '09 at 2:11
 
@Chris - a friend discovered that the hard way when he built a site that managed user-contributed content via GET requests. Googlebot happily followed all the 'delete' links, with predictable results. – Meredith L. Patterson Aug 4 '09 at 18:59

4 Answers

activeoldestvotes
up vote1down vote

Here is a pretty decent example of the difference:

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

share|edit
  
up vote23down vote

Make sure that you're putting your query string at the end of your URL when doing a GET request.

$qry_str = "?x=10&y=20";$ch = curl_init();// Set query data here with the URLcurl_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 POSTcurl_setopt($ch, CURLOPT_POST, 1);// Set query data here with CURLOPT_POSTFIELDScurl_setopt($ch, CURLOPT_POSTFIELDS, $qry_str);$content = trim(curl_exec($ch));curl_close($ch);print $content;

Note from the curl_setopt() docs for CURLOPT_HTTPGET (emphasis added):

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

share|edit
  
up vote3down voteaccepted

Solved: The problem lies here. I set POST via both _CUSTOMREQUEST and _POST and the _CUSTOMREQUEST persisted as POST while _POST switched to _HTTPGET. The Server assumed the header from _CUSTOMREQUEST to be the right one and came back with a 411.

curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
share|edit
  
up vote5down vote

Add this before calling curl_exec($curl_handle)

curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET');
share|edit
7down votefavorite
3

I have tried switching from a previous Post request to a Get request. Which assumes its a Get but eventually does a post.

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 that's setup to do a POST request. That completes successfully but later on when I try to reuse the connection and switch back to GET using the setopts above it still ends up doing a POST internally with incomplete POST headers. The problem is it believes its doing a GET but ends up putting a POST header without the content-length parameter and the connection fails witha 411 ERROR.

share|edit
 
1 
Be careful with Get requests. Google has fun with them. ;) – Chris Lively Aug 4 '09 at 2:11
 
@Chris - a friend discovered that the hard way when he built a site that managed user-contributed content via GET requests. Googlebot happily followed all the 'delete' links, with predictable results. – Meredith L. Patterson Aug 4 '09 at 18:59

4 Answers

activeoldestvotes
up vote1down vote

Here is a pretty decent example of the difference:

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

share|edit
  
up vote23down vote

Make sure that you're putting your query string at the end of your URL when doing a GET request.

$qry_str = "?x=10&y=20";$ch = curl_init();// Set query data here with the URLcurl_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 POSTcurl_setopt($ch, CURLOPT_POST, 1);// Set query data here with CURLOPT_POSTFIELDScurl_setopt($ch, CURLOPT_POSTFIELDS, $qry_str);$content = trim(curl_exec($ch));curl_close($ch);print $content;

Note from the curl_setopt() docs for CURLOPT_HTTPGET (emphasis added):

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

share|edit
  
up vote3down voteaccepted

Solved: The problem lies here. I set POST via both _CUSTOMREQUEST and _POST and the _CUSTOMREQUEST persisted as POST while _POST switched to _HTTPGET. The Server assumed the header from _CUSTOMREQUEST to be the right one and came back with a 411.

curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
share|edit
  
up vote5down vote

Add this before calling curl_exec($curl_handle)

curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET');
share|edit