国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

PHP封裝的HttpClient類,用法實例

2019-11-02 14:46:28
字體:
來源:轉載
供稿:網友

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

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

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 <?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值 // 下面三個屬性現在未實現 public $expires; // 過期時間 public $path; // 路徑 public $domain; // 域 // 從Cookie字符串創建一個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 = ""; } // 設置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; // 協議,http public $port; // 端口 public $header; // 請求頭 public $body; // 請求正文 // 設置頭 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.1rn"; $res .= "Host: $hostrn"; if ($this->header) { reset($this->header); while (list ($k, $v) = each($this->header)) { if (isset ($v) && strlen($v) > 0) $res .= "$k: $vrn"; } } $res .= "rn"; if ($this->body) { $res .= $this->body; $res .= "rnrn"; } return $res; } } // Http響應 class SinHttpResponse { public $scheme; // 協議 public $stasus; // 狀態,成功的時候是ok public $code; // 狀態碼,成功的時候是200 public $header; // 響應頭 public $body; // 響應正文 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; // 響應 public $debug = false; // 是否在Debug模式, //為true的時候會打印出請求內容和相同的頭部 function __construct() { $this->request = new SinHttpRequest(); $this->response = new SinHttpResponse(); $this->context = new SinHttpContext(); $this->timeout = 15; // 默認的超時為15s } // 清除上一次的請求內容 function clearRequest() { $this->request->body = ''; $this->request->setHeader('Content-Length', false); $this->request->setHeader('Content-Type', false); } // post方法 // data為請求的數據 // 為鍵值對的時候模擬表單提交 // 其他時候為數據提交,提交的形式為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";
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台州市| 齐河县| 佛坪县| 陆良县| 白城市| 新建县| 白山市| 灌阳县| 西吉县| 乌拉特前旗| 聂拉木县| 高密市| 麦盖提县| 买车| 嘉义县| 故城县| 南城县| 郑州市| 凤台县| 商南县| 天全县| 南召县| 靖江市| 乌兰县| 修水县| 彰化市| 鱼台县| 汉中市| 武清区| 香河县| 静海县| 喀喇沁旗| 牙克石市| 仙游县| 建瓯市| 贡嘎县| 察哈| 和龙市| 肇源县| 宁南县| 新龙县|