首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 實作輕量級 HTTP Client 類別:支援 GET/POST、Cookie、Header 自訂

     編輯:PHP 綜合知識

設計目標

封裝 PHP 串流上下文 (stream_context_create) 實作簡易 HTTP Client,支援 GET/POST、自訂 Header、Cookie、Referer、User-Agent、請求/回應 Header 記錄,無需 cURL 擴展即可運作。

完整類別原始碼

<?php
class HttpClient {
    public $buffer = '';      // 回應內容
    public $referer = '';     // Referer 標頭
    public $response = '';    // 回應 Header
    public $request = '';     // 請求 Header
    private $options = [];    // 建構選項

    public static function init(?HttpClient &$instance, array $options = []): HttpClient {
        return $instance = new self($options);
    }

    private function __construct(array $options = []) {
        $this->options = $options;
        if (!empty($this->options['debug'])) {
            ob_end_clean();
            set_time_limit(0);
            header('Content-Type: text/plain; charset=utf-8');
        }
    }

    /** 發送 GET 請求 */
    public function get(string $url, ?string $query = null, ?string $cookie = null): ?string {
        $parsed = parse_url($url);
        $host = $parsed['host'] ?? '';
        
        // 組合查詢字串
        if ($query) {
            $url .= (strpos($url, '?') === false ? '?' : '&') . $query;
        }

        $header = $this->build_header($host, $cookie);
        $context = $this->create_context('GET', $header);

        $this->request = $header;
        $this->response = implode("\r\n", get_headers($url) ?: []);
        $this->buffer = @file_get_contents($url, false, $context);
        
        return $this->buffer;
    }

    /** 發送 POST 請求 */
    public function post(string $url, ?string $data = null, ?string $cookie = null): ?string {
        $parsed = parse_url($url);
        $host = $parsed['host'] ?? '';

        $header = $this->build_header($host, $cookie);
        if ($data) {
            $header .= "Content-Length: " . strlen($data) . "\r\n";
        }

        $context = $this->create_context('POST', $header, $data);
        
        $this->request = $header;
        $this->response = implode("\r\n", get_headers($url) ?: []);
        $this->buffer = @file_get_contents($url, false, $context);
        
        return $this->buffer;
    }

    private function build_header(string $host, ?string $cookie): string {
        $ua = $this->options['user_agent'] ?? ($_SERVER['HTTP_USER_AGENT'] ?? 'PHP-HttpClient/1.0');
        $h  = "Host: $host\r\n";
        $h .= "Connection: close\r\n";
        $h .= "Accept: */*\r\n";
        $h .= "User-Agent: $ua\r\n";
        $h .= "DNT: 1\r\n";
        if ($cookie) $h .= "Cookie: $cookie\r\n";
        if ($this->referer) $h .= "Referer: {$this->referer}\r\n";
        return $h;
    }

    private function create_context(string $method, string $header, ?string $content = null): resource {
        $opts = [
            'http' => [
                'method'  => $method,
                'header'  => $header,
                'ignore_errors' => true, // 取得非 2xx 回應內容
            ]
        ];
        if ($content) $opts['http']['content'] = $content;
        return stream_context_create($opts);
    }
}

使用範例

<?php
require 'HttpClient.class.php';

// 初始化(可設定 debug、user_agent)
HttpClient::init($client, [
    'debug'      => true,
    'user_agent' => 'Mozilla/5.0 (compatible; MyBot/1.0)',
]);

// GET 請求
$client->get('https://httpbin.org/get', 'name=test&page=1');
echo "=== Request Header ===\n{$client->request}\n";
echo "=== Response Header ===\n{$client->response}\n";
echo "=== Body ===\n{$client->buffer}\n";

// POST 請求
$client->post('https://httpbin.org/post', 'field1=value1&field2=value2', 'session=abc123');
echo $client->buffer;

// 設定 Referer 後續請求
$client->referer = 'https://example.com/from-page';
$client->get('https://httpbin.org/headers');

功能對照表

功能HttpClient (stream)cURL
GET/POST✓✓
自訂 Header✓✓
Cookie 管理手動自動 (CookieJar)
檔案上傳困難✓ (CURLFile)
HTTPS/SSL 驗證依賴 php.ini細緻控制
代理伺服器stream context✓
非同步/多工✗✓ (curl_multi)
效能中等高

適用場景

  • 簡單 API 串接、網頁抓取、無 cURL 擴展的環境
  • 輕量級專案、不想引入重型依賴
  • 教學示範 HTTP 協定運作原理

擴展建議

  • 加入 put(), delete(), patch() 方法
  • 整合 JSON 自動編碼/解碼
  • 實作 PSR-18 ClientInterface 相容框架
  • 加入重試機制、超時控制、中介軟體管線
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved