亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP封裝的HttpClient類用法實例

 更新時間:2015年06月17日 09:50:28   作者:RobinTang  
這篇文章主要介紹了PHP封裝的HttpClient類,可實現(xiàn)簡單的GET、POST、Cookie、Session等功能,需要的朋友可以參考下

本文實例講述了PHP封裝的HttpClient類。分享給大家供大家參考。具體分析如下:

這是一段php封裝的HttpClient類,可實現(xiàn)GET POST Cookie Session等簡單的功能。原來做過,這兩天重新修改了一下。

<?php 
/* 
 * Filename: httpclient.php 
 * Created on 2012-12-21 
 * Created by RobinTang 
 * To change the template for this generated file go to 
 * Window - Preferences - PHPeclipse - PHP - Code Templates 
 */ 
class SinCookie { 
  public $name; // Cookie名稱 
  public $value; // Cookie值 
  // 下面三個屬性現(xiàn)在未實現(xiàn) 
  public $expires; // 過期時間 
  public $path; // 路徑 
  public $domain; // 域 
  // 從Cookie字符串創(chuàng)建一個Cookie對象 
  function __construct($s = false) { 
    if ($s) { 
      $i1 = strpos($s, '='); 
      $i2 = strpos($s, ';'); 
      $this->name = trim(substr($s, 0, $i1)); 
      $this->value = trim(substr($s, $i1 +1, $i2 - $i1 -1)); 
    } 
  } 
  // 獲取Cookie鍵值對 
  function getKeyValue() { 
    return "$this->name=$this->value"; 
  } 
} 
// 會話上下文 
class SinHttpContext { 
  public $cookies; // 會話Cookies 
  public $referer; // 前一個頁面地址 
  function __construct() { 
    $this->cookies = array (); 
    $this->refrer = ""; 
  } 
  // 設(shè)置Cookie 
  function cookie($key, $val) { 
    $ck = new SinCookie(); 
    $ck->name = $key; 
    $ck->value = $val; 
    $this->addCookie($ck); 
  } 
  // 添加Cookie 
  function addCookie($ck) { 
    $this->cookies[$ck->name] = $ck; 
  } 
  // 獲取Cookies字串,請求時用到 
  function cookiesString() { 
    $res = ''; 
    foreach ($this->cookies as $ck) { 
      $res .= $ck->getKeyValue() . ';'; 
    } 
    return $res; 
  } 
} 
// Http請求對象 
class SinHttpRequest { 
  public $url; // 請求地址 
  public $method = 'GET'; // 請求方法 
  public $host; // 主機 
  public $path; // 路徑 
  public $scheme; // 協(xié)議,http 
  public $port; // 端口 
  public $header; // 請求頭 
  public $body; // 請求正文 
  // 設(shè)置頭 
  function setHeader($k, $v) { 
    if (!isset ($this->header)) { 
      $this->header = array (); 
    } 
    $this->header[$k] = $v; 
  } 
  // 獲取請求字符串 
  // 包含頭和請求正文 
  // 獲取之后直接寫socket就行 
  function reqString() { 
    $matches = parse_url($this->url); 
    !isset ($matches['host']) && $matches['host'] = ''; 
    !isset ($matches['path']) && $matches['path'] = ''; 
    !isset ($matches['query']) && $matches['query'] = ''; 
    !isset ($matches['port']) && $matches['port'] = ''; 
    $host = $matches['host']; 
    $path = $matches['path'] ? $matches['path'] . ($matches['query'] ? '?' . $matches['query'] : '') : '/'; 
    $port = !empty ($matches['port']) ? $matches['port'] : 80; 
    $scheme = $matches['scheme'] ? $matches['scheme'] : 'http'; 
    $this->host = $host; 
    $this->path = $path; 
    $this->scheme = $scheme; 
    $this->port = $port; 
    $method = strtoupper($this->method); 
    $res = "$method $path HTTP/1.1\r\n"; 
    $res .= "Host: $host\r\n"; 
    if ($this->header) { 
      reset($this->header); 
      while (list ($k, $v) = each($this->header)) { 
        if (isset ($v) && strlen($v) > 0) 
          $res .= "$k: $v\r\n"; 
      } 
    } 
    $res .= "\r\n"; 
    if ($this->body) { 
      $res .= $this->body; 
      $res .= "\r\n\r\n"; 
    } 
    return $res; 
  } 
} 
// Http響應(yīng) 
class SinHttpResponse { 
  public $scheme; // 協(xié)議 
  public $stasus; // 狀態(tài),成功的時候是ok 
  public $code; // 狀態(tài)碼,成功的時候是200 
  public $header; // 響應(yīng)頭 
  public $body; // 響應(yīng)正文 
  function __construct() { 
    $this->header = array (); 
    $this->body = null; 
  } 
  function setHeader($key, $val) { 
    $this->header[$key] = $val; 
  } 
} 
// HttpClient 
class SinHttpClient { 
  public $keepcontext = true; // 是否維持會話 
  public $context; // 上下文 
  public $request; // 請求 
  public $response; // 響應(yīng) 
  public $debug = false;
  // 是否在Debug模式,
  //為true的時候會打印出請求內(nèi)容和相同的頭部 
  function __construct() { 
    $this->request = new SinHttpRequest(); 
    $this->response = new SinHttpResponse(); 
    $this->context = new SinHttpContext(); 
    $this->timeout = 15; // 默認的超時為15s 
  } 
  // 清除上一次的請求內(nèi)容 
  function clearRequest() { 
    $this->request->body = ''; 
    $this->request->setHeader('Content-Length', false); 
    $this->request->setHeader('Content-Type', false); 
  } 
  // post方法 
  // data為請求的數(shù)據(jù) 
  // 為鍵值對的時候模擬表單提交 
  // 其他時候為數(shù)據(jù)提交,提交的形式為xml 
  // 如有其他需求,請自行擴展 
  function post($url, $data = false) { 
    $this->clearRequest(); 
    if ($data) { 
      if (is_array($data)) { 
        $con = http_build_query($data); 
        $this->request->setHeader('Content-Type', 'application/x-www-form-urlencoded'); 
      } else { 
        $con = $data; 
        $this->request->setHeader('Content-Type', 'text/xml; charset=utf-8'); 
      } 
      $this->request->body = $con; 
      $this->request->method = "POST"; 
      $this->request->setHeader('Content-Length', strlen($con)); 
    } 
    $this->startRequest($url); 
  } 
  // get方法 
  function get($url) { 
    $this->clearRequest(); 
    $this->request->method = "GET"; 
    $this->startRequest($url); 
  } 
  // 該方法為內(nèi)部調(diào)用方法,不用直接調(diào)用 
  function startRequest($url) { 
    $this->request->url = $url; 
    if ($this->keepcontext) { 
      // 如果保存上下文的話設(shè)置相關(guān)信息 
      $this->request->setHeader('Referer', $this->context->refrer); 
      $cks = $this->context->cookiesString(); 
      if (strlen($cks) > 0) 
        $this->request->setHeader('Cookie', $cks); 
    } 
    // 獲取請求內(nèi)容 
    $reqstring = $this->request->reqString(); 
    if ($this->debug) 
      echo "Request:\n$reqstring\n"; 
    try { 
      $fp = fsockopen($this->request->host, $this->request->port, $errno, $errstr, $this->timeout); 
    } catch (Exception $ex) { 
      echo $ex->getMessage(); 
      exit (0); 
    } 
    if ($fp) { 
      stream_set_blocking($fp, true); 
      stream_set_timeout($fp, $this->timeout); 
      // 寫數(shù)據(jù) 
      fwrite($fp, $reqstring); 
      $status = stream_get_meta_data($fp); 
      if (!$status['timed_out']) { //未超時 
        // 下面的循環(huán)用來讀取響應(yīng)頭部 
        while (!feof($fp)) { 
          $h = fgets($fp); 
          if ($this->debug) 
            echo $h; 
          if ($h && ($h == "\r\n" || $h == "\n")) 
            break; 
          $pos = strpos($h, ':'); 
          if ($pos) { 
            $k = strtolower(trim(substr($h, 0, $pos))); 
            $v = trim(substr($h, $pos +1)); 
            if ($k == 'set-cookie') { 
              // 更新Cookie 
              if ($this->keepcontext) { 
                $this->context->addCookie(new SinCookie($v)); 
              } 
            } else { 
              // 添加到頭里面去 
              $this->response->setHeader($k, $v); 
            } 
          } else { 
            // 第一行數(shù)據(jù) 
            // 解析響應(yīng)狀態(tài) 
            $preg = '/^(\S*) (\S*) (.*)$/'; 
            preg_match_all($preg, $h, $arr); 
            isset ($arr[1][0]) & $this->response->scheme = trim($arr[1][0]); 
            isset ($arr[2][0]) & $this->response->stasus = trim($arr[2][0]); 
            isset ($arr[3][0]) & $this->response->code = trim($arr[3][0]); 
          } 
        } 
        // 獲取響應(yīng)正文長度 
        $len = (int) $this->response->header['content-length']; 
        $res = ''; 
        // 下面的循環(huán)讀取正文 
        while (!feof($fp) && $len > 0) { 
          $c = fread($fp, $len); 
          $res .= $c; 
          $len -= strlen($c); 
        } 
        $this->response->body = $res; 
      } 
      // 關(guān)閉Socket 
      fclose($fp); 
      // 把返回保存到上下文維持中 
      $this->context->refrer = $url; 
    } 
  } 
} 
// demo 
// now let begin test it 
$client = new SinHttpClient(); // create a client 
$client->get('http://www.baidu.com/');  // get 
echo $client->response->body; // echo 
?>

希望本文所述對大家的php程序設(shè)計有所幫助。

相關(guān)文章

  • php cookie使用方法學習筆記分享

    php cookie使用方法學習筆記分享

    在php中cookie與其它程序中沒什么兩樣,cookie都是用來存儲信息到客戶端,常用用于安全性要求不高的一些應(yīng)用中,如用戶登錄記住密碼之類的,下面我來給大家介紹一下php cookie學習筆記
    2013-11-11
  • PHP 數(shù)字左側(cè)自動補0

    PHP 數(shù)字左側(cè)自動補0

    舉例來說:員工的工號 X001 雖然 MySQL 本身的字段設(shè)定成 zerofill 的屬性時,會將不足位數(shù)的部份補上0,但是這個功能只能用在數(shù)字的字段上面。 所以必要的時候只好靠PHP來進行了。
    2008-03-03
  • Ezpop?pop序列化鏈反序列化知識

    Ezpop?pop序列化鏈反序列化知識

    這篇文章主要為大家介紹了Ezpop?pop序列化鏈反序列化知識,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04
  • PHP通過GD庫實現(xiàn)驗證碼功能示例

    PHP通過GD庫實現(xiàn)驗證碼功能示例

    這篇文章主要介紹了PHP通過GD庫實現(xiàn)驗證碼功能,結(jié)合實例形式分析了php驗證碼相關(guān)圖片繪制、輸出操作實現(xiàn)技巧,需要的朋友可以參考下
    2019-02-02
  • php實現(xiàn)的Curl封裝類Curl.class.php用法實例分析

    php實現(xiàn)的Curl封裝類Curl.class.php用法實例分析

    這篇文章主要介紹了php實現(xiàn)的Curl封裝類Curl.class.php用法,以完整實例形式較為詳細的分析了Curl封裝類的定義及相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • PHP頁面轉(zhuǎn)UTF-8中文編碼亂碼的解決辦法

    PHP頁面轉(zhuǎn)UTF-8中文編碼亂碼的解決辦法

    這篇文章主要介紹了php中uft-8中文編碼亂碼的解決辦法,需要的朋友可以參考下
    2015-10-10
  • 基于PHP開發(fā)中的安全防范知識詳解

    基于PHP開發(fā)中的安全防范知識詳解

    本篇文章是對PHP開發(fā)中的安全防范知識進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • 高級php注入方法集錦

    高級php注入方法集錦

    不論是黑友,還是程序員,都需要了解
    2008-07-07
  • php 定義404頁面的實現(xiàn)代碼

    php 定義404頁面的實現(xiàn)代碼

    今天需要用php輸出一個404錯誤頁面,然后加載404頁面輸出,各個方便給用戶與搜索引擎好的用戶體驗
    2012-11-11
  • php的$_FILES的臨時儲存文件與回收機制實測過程

    php的$_FILES的臨時儲存文件與回收機制實測過程

    上傳文件是怎么個原理,大概的想了下,應(yīng)該是一種回收機制:點擊了臨時文件空間,那么,php自身應(yīng)該自己維護這塊空間的回收,具體的測試過程如下,感興趣的朋友可以參考下哈
    2013-07-07

最新評論