2011-11-16 65 views
3

我正在使用curl發佈到腳本。捲曲:*違反RFC 2616/10.3.2並從POST切換到GET

curl_setopt ($ch, CURLOPT_POST, true); 
curl_setopt($ch,CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); 

但是有301重定向涉及哪些casues curl從POST切換到GET。

 
HTTP/1.1 301 Moved Permanently 
< Location: https://myserver.org/php/callback-f.php 
< Content-Length: 0 
< Date: Wed, 16 Nov 2011 17:21:06 GMT 
< Server: lighttpd/1.4.28 
* Connection #0 to host myserver.org left intact 
* Issue another request to this URL: 'https://myserver.org/php/callback-f.php' 
* Violate RFC 2616/10.3.2 and switch from POST to GET 
* About to connect() to myserver.org port 443 

有誰知道我可以如何防止curl切換到GET請?

回答

5

CURLOPT_POSTREDIR可以被設置爲配置此行爲(請求方法,用於在捲曲301位置報頭的基於自動重定向):

curl_setopt(,CURLOPT_POSTREDIR,3);

這裏3告訴curl模塊重定向301以及302請求。

0,1,2,3是最後一個參數的有效選項。

0 - >不設置任何行爲
1 - >遵循只有301重定向的相同類型的請求重定向。
2 - >遵循重定向,只有302重定向具有相同類型的請求。
3 - >跟隨301和302重定向的同一請求類型的重定向。

看得那麼清楚:Request #49571 CURLOPT_POSTREDIR not implemented其中有一些有用的意見,比如設置一個自定義的請求方法:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
+0

謝謝,看起來這可能是一個。明天會看看。 – rix

2

the latest draft of HTTP

注:由於歷史原因,用戶代理可以請求方法 從POST更改以獲取後續請求。如果此行爲是不期望的 ,則可以改爲使用狀態碼307(臨時重定向)。

我覺得303 See Other也許也適合。

0

好,面臨着重新編譯PHP的,因爲我的嫋嫋不支持POSTREDIR我解決解決這個與JQuery。希望這可以幫助別人!

<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script> 

$.post("path/path/callback.php", { "key":"value", "key2":"value2"}); 

</script> 
</head> 
hello 

</html>