1. 程式人生 > >PHP利用socket傳送HTTP請求(封裝好的類)

PHP利用socket傳送HTTP請求(封裝好的類)

<?php

/**

 * 使用PHP Socket 程式設計模擬Http post和get請求

 * @author koma

 */

class Http{

  private $sp = "\r\n"; //這裡必須要寫成雙引號

  private $protocol = 'HTTP/1.1';

  private $requestLine = "";

  private $requestHeader = "";

  private $requestBody = "";

  private $requestInfo = "";

  private $fp = null;

  private $urlinfo = null;

  private $header = array();

  private $body = "";

  private $responseInfo = "";

  private static $http = null; //Http物件單例

    

  private function __construct() {}

    

  public static function create() {

    if ( self::$http === null ) {

      self::$http = new Http();

    }

    return self::$http;

  }

    

  public function init($url) {

    $this->parseurl($url);

    $this->header['Host'] = $this->urlinfo['host'];

    return $this;

  }

    

  public function get($header = array()) {

    $this->header = array_merge($this->header, $header);

    return $this->request('GET');

  }

    

  public function post($header = array(), $body = array()) {

    $this->header = array_merge($this->header, $header);

    if ( !empty($body) ) {

      $this->body = http_build_query($body);

      $this->header['Content-Type'] = 'application/x-www-form-urlencoded';

      $this->header['Content-Length'] = strlen($this->body);

    }

    return $this->request('POST');

  }

    

  private function request($method) {

    $header = "";

    $this->requestLine = $method.' '.$this->urlinfo['path'].'?'.$this->urlinfo['query'].' '.$this->protocol;

    foreach ( $this->header as $key => $value ) {

      $header .= $header == "" ? $key.':'.$value : $this->sp.$key.':'.$value;

    }

    $this->requestHeader = $header.$this->sp.$this->sp;

    $this->requestInfo = $this->requestLine.$this->sp.$this->requestHeader;

    if ( $this->body != "" ) {

      $this->requestInfo .= $this->body;

    }

    /*

     * 注意:這裡的fsockopen中的url引數形式為"www.xxx.com"

     * 不能夠帶"http://"這種

     */

    $port = isset($this->urlinfo['port']) ? isset($this->urlinfo['port']) : '80';

    $this->fp = fsockopen($this->urlinfo['host'], $port, $errno, $errstr);

    if ( !$this->fp ) {

      echo $errstr.'('.$errno.')';

      return false;

    }

    if ( fwrite($this->fp, $this->requestInfo) ) {

      $str = "";

      while ( !feof($this->fp) ) {

        $str .= fread($this->fp, 1024);

      }

      $this->responseInfo = $str;

    }

    fclose($this->fp);

    return $this->responseInfo;

  }

    

  private function parseurl($url) {

    $this->urlinfo = parse_url($url);

  }

}

// $url = "http://news.163.com/14/1102/01/AA0PFA7Q00014AED.html";

$url = "http://localhost/httppro/post.php";

$http = Http::create()->init($url);

/* 傳送get請求

echo $http->get(array(

  'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',

));

*/

  

/* 傳送post請求 */

echo $http->post(array(

    'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',

), array('username'=>'發一箇中文', 'age'=>22));