2014-02-24 71 views
0

我正在嘗試開發一個可以處理cookie的完整PHP網頁瀏覽器。我創建了以下類:file_get_contents獲取Cookies

<?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; 
    } 
} 

回答

3

據php.net:

$ http_response_header會在局部範圍內創建。

因此$http_response_header僅在get()範圍內可用。 您可以像$this->get_cookies($http_response_header);那樣傳遞它或創建一個屬性來存儲它。

+0

感謝它完美的作品。我會用答案更新我的問題。 – Manitoba

+0

太棒了!我很高興它幫助;) – wesolyromek