這篇文章將為大家詳細(xì)講解有關(guān)怎么在PHP中對(duì)HttpClient類(lèi)進(jìn)行封裝,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
專業(yè)領(lǐng)域包括成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、商城網(wǎng)站建設(shè)、微信營(yíng)銷(xiāo)、系統(tǒng)平臺(tái)開(kāi)發(fā), 與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開(kāi)發(fā)公司不同,創(chuàng)新互聯(lián)建站的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營(yíng)銷(xiāo)的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。具體分析如下:
<?php class SinCookie { public $name; // Cookie名稱 public $value; // Cookie值 // 下面三個(gè)屬性現(xiàn)在未實(shí)現(xiàn) public $expires; // 過(guò)期時(shí)間 public $path; // 路徑 public $domain; // 域 // 從Cookie字符串創(chuàng)建一個(gè)Cookie對(duì)象 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鍵值對(duì) function getKeyValue() { return "$this->name=$this->value"; } } // 會(huì)話上下文 class SinHttpContext { public $cookies; // 會(huì)話Cookies public $referer; // 前一個(gè)頁(yè)面地址 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字串,請(qǐng)求時(shí)用到 function cookiesString() { $res = ''; foreach ($this->cookies as $ck) { $res .= $ck->getKeyValue() . ';'; } return $res; } } // Http請(qǐng)求對(duì)象 class SinHttpRequest { public $url; // 請(qǐng)求地址 public $method = 'GET'; // 請(qǐng)求方法 public $host; // 主機(jī) public $path; // 路徑 public $scheme; // 協(xié)議,http public $port; // 端口 public $header; // 請(qǐng)求頭 public $body; // 請(qǐng)求正文 // 設(shè)置頭 function setHeader($k, $v) { if (!isset ($this->header)) { $this->header = array (); } $this->header[$k] = $v; } // 獲取請(qǐng)求字符串 // 包含頭和請(qǐng)求正文 // 獲取之后直接寫(xiě)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),成功的時(shí)候是ok public $code; // 狀態(tài)碼,成功的時(shí)候是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; // 是否維持會(huì)話 public $context; // 上下文 public $request; // 請(qǐng)求 public $response; // 響應(yīng) public $debug = false; // 是否在Debug模式, //為true的時(shí)候會(huì)打印出請(qǐng)求內(nèi)容和相同的頭部 function __construct() { $this->request = new SinHttpRequest(); $this->response = new SinHttpResponse(); $this->context = new SinHttpContext(); $this->timeout = 15; // 默認(rèn)的超時(shí)為15s } // 清除上一次的請(qǐng)求內(nèi)容 function clearRequest() { $this->request->body = ''; $this->request->setHeader('Content-Length', false); $this->request->setHeader('Content-Type', false); } // post方法 // data為請(qǐng)求的數(shù)據(jù) // 為鍵值對(duì)的時(shí)候模擬表單提交 // 其他時(shí)候?yàn)閿?shù)據(jù)提交,提交的形式為xml // 如有其他需求,請(qǐng)自行擴(kuò)展 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); } // 獲取請(qǐng)求內(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); // 寫(xiě)數(shù)據(jù) fwrite($fp, $reqstring); $status = stream_get_meta_data($fp); if (!$status['timed_out']) { //未超時(shí) // 下面的循環(huán)用來(lái)讀取響應(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)正文長(zhǎ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 ?>
關(guān)于怎么在PHP中對(duì)HttpClient類(lèi)進(jìn)行封裝就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站標(biāo)題:怎么在PHP中對(duì)HttpClient類(lèi)進(jìn)行封裝-創(chuàng)新互聯(lián)
文章起源:http://jinyejixie.com/article12/dpsegc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、面包屑導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)、建站公司、域名注冊(cè)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容