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



我在尝试使用 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始终有效。

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

我上面的评论中有几个未回答的问题,但出于示例目的,我将分享这段代码。这是我过去用作使用 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...

我的直觉猜测是,你只是在原始代码中缺少一个饼干罐,但这主要是基于直觉,因为我们目前还没有你所有的问题代码。

最新更新