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

标签 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;

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

但是,通过浏览器我得到的 cookie 总是有效的。

有人遇到过类似的问题,不知道怎么解决。

最佳答案

我的上述评论中有几个 Unresolved 问题,但我将分享这段代码作为示例。这是我过去使用 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,但这主要是基于直觉,因为我们目前没有您所有的问题代码。

关于php - 从 CURL 请求和 file_get_contents 收到的无效 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48237093/

相关文章:

php - SilverStripe 4 - DataExtension 上的 has_many 关系

javascript - ajax->Javascript[POST]->php : passed text param not seen in PHP

Android:处理来自 HTTP Get-Request 的 Cookie

ruby-on-rails - 配置 Rails 应用程序以处理多个子域和多个 cookie

http - 在 HTTP 中使用自定义 header 字段

php - img src 不适用于 php 和 php 变量

php - 使用 wget 的多个 cron 作业

java - 为什么我的 "Set-Cookie"响应 header 没有被翻译成实际的 cookie?

javascript - AngularJS:忽略 Content-Type 参数

android - Android 浏览器 <video> 标签请求的请求 header 中没有 Referer