我正在使用curl发布到脚本.
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重定向,其中包括从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
有谁知道我怎么能阻止卷曲切换到GET请?
解决方法:
可以设置CURLOPT_POSTREDIR来配置此行为(基于卷曲的301位置标头自动重定向的请求方法):
curl_setopt( , CURLOPT_POSTREDIR, 3);
here 3 tells curl module to redirect both 301 as well as 302 requests.
0,1,2,3 are the valid options for the last argument.
0 -> do not set any behavior
1 -> follow redirect with the same type of request only for 301 redirects.
2 -> follow redirect with the same type of request only for 302 redirects.
3 -> follow redirect with the same type of request both for 301 and 302 redirects.
请参阅:Request #49571 CURLOPT_POSTREDIR not implemented有一些有用的注释,比如设置自定义请求方法:
curl_setopt( $ch, CURLOPT_CUSTomrEQUEST, "POST");