file_get_contents获取Cookies [英] file_get_contents get Cookies

查看:62
本文介绍了file_get_contents获取Cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to develop a full PHP web browser that can handle cookies. I've created the following class:

<?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。
我做了一个非常简单的测试脚本:

Basically, it can send a GET request and 'should' retrieve the cookies. I've made a very simple test script:

<?php

require('browser.class.php');

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

但是它在第 32 行失败,因为 $ http_response_header 看起来为空。它不应该包含我回复的标题吗?我已经读过该页面,但是对于这个人来说看起来很不错:在PHP中获取带有file_get_contents的cookie

But it fails on line 32 as $http_response_header looks to be null. Isn't it supposed to contain the header of my response ? I've read that page but it looks to work well for this guy : get cookie with file_get_contents in PHP

我知道我可以使用 cUrl 来处理它,但是我d非常喜欢使用粗糙的PHP代码。

I know I could use cUrl to handle this but I'd really like to use a rough PHP code.

我做错了吗?

感谢您的宝贵

编辑:

解决方案是:

<?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.net:


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

$http_response_header will be created in the local scope.

因为此 $ http_response_header 仅在 get()。
您可以像 $ this-> get_cookies($ http_response_header); 这样传递它,也可以创建一个属性来存储它。

Because of this $http_response_header is available only in the scope of get(). You can pass it like $this->get_cookies($http_response_header); or create a property to store it.

这篇关于file_get_contents获取Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆