CURL响应代码是200而不是302

时间:2013-01-08 11:11:30

标签: php https cron

我有以下代码:

    $curl=curl_init();      
    $allowAppPostPArams = 'utf8=%E2%9C%93&authenticity_token='.$this->authToken.'&permissions=manage_likes%2Cmanage_relationships%2Cmanage_comments%2C&response_type=code&app_id=d2a4fc6d2751&redirect_uri=http%3A%2F%2FsocialCamFollower%2Findex.php&choice=authorize&commit=Authorize';
    curl_setopt($curl, CURLOPT_POSTFIELDS, $allowAppPostPArams);
    curl_setopt($curl, CURLOPT_POST, 1);

    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);

    curl_setopt($curl, CURLOPT_URL, 'https://socialcam.com/oauth/form_submit');        
    curl_setopt($curl, CURLOPT_REFERER, $urlAppPage);

    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20100101 Firefox/17.0');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    curl_close($curl);

浏览器的请求: enter image description here

CURL LOG curl log

  

问题:为什么CURL显示200而不是302?

服务器:MAMP,Apache2,php5.3.14。安全模式& open_basedir未设置。

1 个答案:

答案 0 :(得分:1)

使用此

curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
...
curl_exec($ch);
...
function read_header($curl, $header) {
  echo $header;
  return strlen($header);
}

启用CURLOPT_FOLLOWLOCATION后,您应该获得类似

的输出
HTTP/1.0 301 Moved Permanently
Date: Fri, 20 Apr 2012 11:26:37 GMT
Server: Apache
Location: http://www.spiegel.de/
Content-Length: 230
Content-Type: text/html; charset=iso-8859-1
X-Cache: MISS from lnxp-3968.srv.mediaways.net
X-Cache-Lookup: MISS from lnxp-3968.srv.mediaways.net:91
Via: 1.0 lnxp-3968.srv.mediaways.net (squid/3.1.4)
Connection: close

HTTP/1.0 200 OK
Date: Fri, 20 Apr 2012 11:25:38 GMT
Server: Apache-Coyote/1.1
X-Powered-By: Servlet 2.4; JBoss-4.0.3SP1 (build: CVSTag=JBoss_4_0_3_SP1 date=200510231054)/Tomcat-5.5
Cache-Control: max-age=120
Expires: Fri, 20 Apr 2012 11:27:38 GMT
X-Host: lnxp-2885
X-Robots-Tag: index, follow, noarchive
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 161305
Vary: Accept-Encoding
Age: 59
X-Cache: HIT from lnxp-3954.srv.mediaways.net
X-Cache-Lookup: HIT from lnxp-3954.srv.mediaways.net:90
Via: 1.1 www.spiegel.de, 1.0 lnxp-3954.srv.mediaways.net (squid/3.1.4)
Connection: close