【问题标题】:file_get_contents get Cookiesfile_get_contents 获取 Cookie
【发布时间】:2014-03-25 21:37:30
【问题描述】:

我正在尝试开发一个可以处理 cookie 的完整 PHP 网络浏览器。我创建了以下类:

<?php

class Browser
{
  private $cookies = '';
  private $response_cookies = '';
  private $content = '';

  /**
   * Cookie manager
   * @Description : To set or get cookies as Array or String
   */
  public function set_cookies_json($cookies)
  {
    $cookies_json = json_decode($cookies, true);
    $cookies_array = array();
    foreach ($cookies_json as $key => $value)
    {
      $cookies_array[] = $key .'='.$value;
    }
    $this->cookies = 'Cookie: ' . $cookies_array.join('; ') . "\r\n";
  }

  public function set_cookies_string($cookies)
  {
    $this->cookies = 'Cookie: ' . $cookies . "\r\n";
  }

  private function get_cookies()
  {
    global $http_response_header;
    $cookies_array = array();
    foreach($http_response_header as $s)
    {
      if (preg_match('|^Set-Cookie:\s*([^=]+)=([^;]+);(.+)$|', $s, $parts))
      {
        $cookies_array[] = $parts[1] . '=' . $parts[2];
      }
    }

    $this->cookies = 'Cookie: ' . $cookies_array.join('; ') . "\r\n";
  }

  /**
   * GET and POST request
   * Send a GET or a POST request to a remote URL
   */
  public function get($url)
  {
    $opts = array(
      'http' => array(
        'method' => 'GET',
        'header' => "Accept-language: en\r\n" .
                    $this->cookies
      )
    );
    $context = stream_context_create($opts);
    $this->content = file_get_contents($url, false, $context);
    $this->get_cookies();
    return $this->content;
  }

  public function post($url, $post_data)
  {
    $post_content = array();
    foreach ($post_data as $key => $value)
    {
      $post_content[] = $key .'='.$value;
    }

    $opts = array(
      'http' => array(
        'method' => 'GET',
        'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
                    $this->cookies,
        'content' => $post_content.join('&'),
      )
    );
    $context = stream_context_create($opts);
    $this->content = file_get_contents($url, false, $context);
    $this->get_cookies();
    return $this->content;
  }
}

基本上,它可以发送 GET 请求并“应该”检索 cookie。 我做了一个非常简单的测试脚本:

<?php

require('browser.class.php');

$browser = new Browser();
$browser->get('http://google.com');
print_r($browser->response_cookies);

但它在32 线上失败,因为$http_response_header 看起来为空。它不应该包含我的响应的标题吗?我已经阅读了该页面,但它看起来很适合这个人:get cookie with file_get_contents in PHP

我知道我可以使用 cUrl 来处理这个问题,但我真的很想使用粗略的 PHP 代码。

我是不是做错了什么?

感谢您的宝贵帮助。

编辑:

解决办法是:

<?php

class Browser
{
  public $request_cookies = '';
  public $response_cookies = '';
  public $content = '';

  /**
   * Cookie manager
   * @Description : To set or get cookies as Array or String
   */
  public function set_cookies_json($cookies)
  {
    $cookies_json = json_decode($cookies, true);
    $cookies_array = array();
    foreach ($cookies_json as $key => $value)
    {
      $cookies_array[] = $key .'='.$value;
    }
    $this->request_cookies = 'Cookie: ' . join('; ', $cookies_array) . "\r\n";
  }

  public function set_cookies_string($cookies)
  {
    $this->request_cookies = 'Cookie: ' . $cookies . "\r\n";
  }

  private function get_cookies($http_response_header)
  {
    $cookies_array = array();
    foreach($http_response_header as $s)
    {
      if (preg_match('|^Set-Cookie:\s*([^=]+)=([^;]+);(.+)$|', $s, $parts))
      {
        $cookies_array[] = $parts[1] . '=' . $parts[2];
      }
    }

    $this->response_cookies = 'Cookie: ' . join('; ', $cookies_array) . "\r\n";
  }

  /**
   * GET and POST request
   * Send a GET or a POST request to a remote URL
   */
  public function get($url)
  {
    $opts = array(
      'http' => array(
        'method' => 'GET',
        'header' => "Accept-language: en\r\n" .
                    $this->request_cookies
      )
    );
    $context = stream_context_create($opts);
    $this->content = file_get_contents($url, false, $context);
    $this->get_cookies($http_response_header);
    return $this->content;
  }

  public function post($url, $post_data)
  {
    $post_content = array();
    foreach ($post_data as $key => $value)
    {
      $post_content[] = $key .'='.$value;
    }

    $opts = array(
      'http' => array(
        'method' => 'GET',
        'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
                    $this->request_cookies,
        'content' => join('&', $post_content),
      )
    );
    $context = stream_context_create($opts);
    $this->content = file_get_contents($url, false, $context);
    $this->get_cookies($http_response_header);
    return $this->content;
  }
}

【问题讨论】:

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


    【解决方案1】:

    根据php.net:

    $http_response_header 将在本地范围内创建。

    因为这个$http_response_header 只能在get() 的范围内使用。 您可以像 $this-&gt;get_cookies($http_response_header); 一样传递它,或者创建一个属性来存储它。

    【讨论】:

    • 感谢它完美运行。我会用答案更新我的问题。
    • 太棒了!我很高兴它有所帮助;)
    猜你喜欢
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多