CURL(PHP)中的Cookie问题 - CURL标头中未附加Cookie信息

时间:2016-09-22 16:30:38

标签: php curl cookies header

我有这段代码

<?php
    $mLoginUrl = "https://www.test.com/login";
    $mCookieFile = dirname(__FILE__).'/tmpCookies/cookie'.rand().'.txt';

    define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
    define('COOKIE_FILE', $mCookieFile);
    define('LOGIN_FORM_URL', $mLoginUrl);
    define('LOGIN_ACTION_URL', $mLoginUrl);

    $postValues = array(
        'user[email]' => "mymail@email.com",
        'user[password]' => "mypassword"
    );

    $headers = Array(
                "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
                "Cache-Control: max-age=0",
                "Connection: keep-alive",
                "Keep-Alive: 300",
                "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                "Accept-Language: en-us,en;q=0.5",
                "Pragma: "
            );

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
    curl_setopt($curl, CURLOPT_URL, LOGIN_ACTION_URL);
    curl_setopt($curl, CURLOPT_POST, TRUE);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
    curl_setopt($curl, CURLOPT_COOKIEFILE, COOKIE_FILE);
    curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_REFERER, LOGIN_FORM_URL);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_HTTPHEADER,$headers) ;
    curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
    $res = curl_exec($curl);
    $info = curl_getinfo($curl);
    print_r($info['request_header']);
    exit;
?> 

这在我的本地计算机和我的一台服务器上正常工作,并在输出后显示

GET / HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36
Host: www.test.com
Referer: https://www.test.com/login
Cookie: _property_session=WjROWEYvTHNYaE5Zb29jVk04WGM0Z3FybmhmY1ZIeVdBc2N6d2N3UmViaXlZdFNhR1dSbUN4QVh6aFFSRjFPYktybmdnRGlXNG0yWWcremEzcklKTnE1ZE1lTTM0eUQrSG90SVhRRzhvYW5rWmFQTVhBMjVCWjBtb1FSc0RrTEh2RjhHSFI3aHkwa3U4N3Y3czJhTzJuN2ZGbWRRN0Nra2Z6OTR4aHhvVG42bVVRS3kwTExUL1hMN2JoZ0xRd2g3VVdIMC81cGhLQzJjOTJvc2RYajIwakE0VjZqRnhTeHBleFltTGF4Z3hpUGJEb0E3Nlo2S3BwMElqNnVkaWhDVS0tc0pCRlozSVE5bXRHQXlHWE1IbTl4UT09--67cf6e056b84b4cae4d275507f544927802eb78d
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Cache-Control: max-age=0
Connection: keep-aliveKeep-Alive: 300 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Language: en-us,en;q=0.5 

这意味着cookie已经创建并附加在CURL的标题中(我们可以在上面的CURL标题打印中看到。)cookie文件也在上述位置创建。

但是在我的一台服务器上,代码不按预期工作,并提供以下输出

POST /users/sign_in? HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36 Host: www.test.com Referer: https://www.test.com/login Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Cache-Control: max-age=0 Connection: keep-alive Keep-Alive: 300 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Language: en-us,en;q=0.5 Content-Length: 76 Content-Type: application/x-www-form-urlencoded 

表示Cookie信息未附加在CURL的标头中。我已经检查过在这个(有问题的)服务器中创建了cookie文件并且有cookie内容,但是CURL头中还没有包含cookie内容。提到的临时cookie目录具有所有用户的完全权限/权限(777)。

在有问题的服务器上,PHP版本为5.4.19,CURL版本为7.19.7。

如果有人可以帮忙吗?我尝试过在互联网上找到的所有解决方案。

提前致谢。

2 个答案:

答案 0 :(得分:0)

$mCookieFile = dirname(__FILE__).'/tmpCookies/cookie'.rand().'.txt';

删除rand()...为静态文件

答案 1 :(得分:0)

感谢大家的帮助。我已经解决了问题。问题是open_basedir路径。我把它设置为&#34; none&#34;在修复问题的服务器上。