从CURL请求和file_get_contents收到的cookie无效

时间:2018-01-13 05:07:59

标签: php cookies http-headers file-get-contents

我在尝试使用file_get_contentscurl捕获Cookie时收到无效的Cookie字符串。直接从浏览器浏览时收到的cookie是有效/活动的。但是,从file_get_contentscurl捕获的Cookie似乎无效。

我试图从file_get_contents这样抓取

 $context =      array(
  'http' => array(
   'method' => 'GET',
   'header' => array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8', 'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/63.0.3239.84 Chrome/63.0.3239.84 Safari/537.36'),
  )
 );

   $cxContext = stream_context_create($context);


    file_get_contents($url, false, $cxContext);
    $cookies = array();

    foreach ($http_response_header as $hdr) {
    if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) {
        $cookies = $matches[1];
    }

}
return $cookies;

我尝试通过设置标题来解决这个问题,但返回的Cookie总是过期或者无效。

但是,通过浏览器,我获得的cookie始终有效。

任何人都遇到类似的问题,不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:3)

我上面的评论中有几个未解答的问题,但我会出于示例目的分享这些代码。它是我过去用作使用cURL进行浏览器模拟的基类的:

<?php
if(!function_exists("curl_init")) { throw new Exception("CurlBrowser requires the cURL extension, which is not enabled!"); }
class CurlBrowser
{
    public $userAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0";
    /*
    Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0
    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0 
    */

    public $cookiesFile = null;
    public $proxyURL = null;
    public $saveLastOutput = "";
    public $caBundle = "cacert.pem";
    public $httpHeaders = array();

    public function __construct($UseCookies = true)
    {
        if(is_bool($UseCookies) && $UseCookies)
        {
            $this->cookiesFile = dirname(__FILE__)."/cookies.txt";
        }
        elseif(is_string($UseCookies) && ($UseCookies != ""))
        {
            $this->cookiesFile = $UseCookies;
        }
    }

    public function SetCustomHTTPHeaders($arrHeaders)
    {
        $this->httpHeaders = $arrHeaders;
    }

    public function SetProxy($proxy)
    {
        $this->proxyURL = $proxy;
    }

    public function Get($url)
    {
        return $this->_request($url);
    }

    public function Post($url,$data = array())
    {
        return $this->_request($url,$data);
    }

    private function _request($form_url,$data = null)
    {
        $ch = curl_init($form_url);

        // CA bundle
        $caBundle = $this->caBundle;
        if(file_exists($caBundle))
        {
            // Detect and convert relative path to absolute path
            if(basename($caBundle) == $caBundle)
            {
                $caBundle = getcwd() . DIRECTORY_SEPARATOR  . $caBundle;
            }
            // Set CA bundle
            curl_setopt($ch, CURLOPT_CAINFO, $caBundle);
        }

        // Cookies
        if($this->cookiesFile !== null)
        {
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiesFile);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiesFile);
        }

        // User Agent
        curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); 

        // Misc
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_ENCODING, "gzip, deflate");

        // Optional proxy
        if($this->proxyURL !== null)
        {
            curl_setopt($ch, CURLOPT_PROXY, $this->proxyURL);
        }

        // Custom HTTP headers
        if(count($this->httpHeaders))
        {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeaders);
        }

        // POST data
        if($data !== null)
        {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }

        // Run operation
        $result = curl_exec($ch);

        if($result === false)
        {
          throw new Exception(curl_error($ch));
        }
        else
        {
            if(!empty($this->saveLastOutput))
            {
            file_put_contents($this->saveLastOutput,$result);
          }
          return $result;
        }
    }
}
?>

您可以这样使用它:

<?php
$browser = new CurlBrowser();
$html = $browser->Get("https://....");
...etc...

我的猜测是,您只是在原始代码中错过了一个cookie jar,但这主要是基于直觉,因为我们此时并未拥有您的所有问题代码