一、組件簡介
Z-BlogPHP 自帶的 NetWork 網(wǎng)絡(luò)組件是從 1.0 版本就開始附帶的自研組件。歷經(jīng)多年的開發(fā)打磨,其功能已經(jīng)非常成熟。
NetWork 組件由 Network 工廠類和 3 個(gè)業(yè)務(wù)組件:Network__curl、Network__filegetcontents、Network__fsockopen 類,分別對應(yīng)了 PHP 里的三種訪問網(wǎng)絡(luò)的方式。使用 Network 工廠類創(chuàng)建網(wǎng)絡(luò)操作組件時(shí),默認(rèn)的創(chuàng)建順序是,如果 curl 擴(kuò)展存在,就初始化 Network__curl;如果 allow_url_fopen 打開,fsockopen 存在,就初始化 Network__fsockopen;最后是返回 Network__filegetcontents。
二、組件方法簡介
1、工廠類方法
- Network::Create():按默認(rèn)順序創(chuàng)建并返回一個(gè)新的網(wǎng)絡(luò)操作組件;
- Network::Create(‘curl’) :創(chuàng)建并返回一個(gè)新的 Network__curl 網(wǎng)絡(luò)操作組件;
- Network::Create(‘fsockopen’) :創(chuàng)建并返回一個(gè)新的 Network__fsockopen 網(wǎng)絡(luò)操作組件;
- Network::Create(‘filegetcontents’) :創(chuàng)建并返回一個(gè)新的 Network__filegetcontents 網(wǎng)絡(luò)操作組件。
2、業(yè)務(wù)組件屬性
- responseText:返回的數(shù)據(jù);
- responseHeader:返回的 Http Header;
- status:返回的狀態(tài)碼;
- statusText:返回的狀態(tài)碼文本。
3、業(yè)務(wù)組件方法
- open($Method, $Url):打開 network 組件;
- send($varBody = ”):network 組件發(fā)送連接;
- setRequestHeader($bstrHeader, $bstrValue):設(shè)置請求 Header;
- enableGzip():啟用 Gzip 壓縮;
- setMaxRedirs($n):設(shè)置最大重定向次數(shù);
- addBinary($name, $entity):添加二進(jìn)制文件上傳;
- addText($name, $entity):添加發(fā)送參數(shù);
- setTimeOuts($resolveTimeout, $connectTimeout, $sendTimeout, $receiveTimeout):設(shè)置超時(shí);
- getAllResponseHeaders():獲取所有的響應(yīng)頭;
- getResponseHeader($name):獲取指定響應(yīng)頭。
4、1.7.2新加入方法
- getStatusCode():獲取響應(yīng)代碼;
- getStatusText():獲取完整的響應(yīng)頭;
- getReasonPhrase():獲取響應(yīng)代碼的解釋;
- getBody():獲取響應(yīng)正文;
- getHeaders:獲取響應(yīng)頭數(shù)組;
- getHeader($name):獲取指定響應(yīng)頭;
- hasHeader($name):判斷指定響應(yīng)頭存在。
二、簡易使用方法
示例代碼 1:GET 方法訪問網(wǎng)頁:
//假定我們抓取so.com $url = 'https://www.so.com/'; $http = Network::Create(); $http->open('GET', $url); $http->enableGzip();//打開gzpi $http->setTimeOuts(120, 120, 0, 0);//設(shè)置超時(shí) $http->send(); //獲取訪問后的正文 $txt = $http->responseText; //獲取HTTP響應(yīng)代碼 $code = $http->status;//正常訪問為200
示例代碼 2:POST 方法訪問網(wǎng)頁:
//假定我們向某頁面提交登錄 $url = 'https://test/login.php'; $data = array(); $data['username'] = '神馬'; $data['password'] = md5('都是浮云'); $http = Network::Create(); $http->open('POST', $url); $http->enableGzip();//打開gzpi $http->setTimeOuts(120, 120, 0, 0);//設(shè)置超時(shí) $http->send($data); $txt = $http->responseText; echo $txt;
示例代碼 3:設(shè)置 RequestHeader,User-Agent,Cookie 頭:
$u = 'ZBlogPHP/173000';//user-agent $c = ' name=' . urlencode('nobody');//cookies name 為nobody $c .= ' ;password=' . urlencode('123456');//cookies password 為123456 //在send之前,設(shè)置setRequestHeader $http->setRequestHeader('User-Agent', $u); $http->setRequestHeader('Cookie', $c);
三、高級使用方法
示例代碼 4:指定重定向次數(shù)
//NetWork組件默認(rèn)重定向次數(shù)是0次,也就是遇到301,302就停 $url = 'https://test/http302.php'; $http = Network::Create(); $http->open('GET', $url); //設(shè)定重定向次數(shù)是5次,說明最多可以重定向5次,可避免死循環(huán)。 $http->setMaxRedirs(5); $http->send();
示例代碼 5:上傳文件:
$url = 'https://test/upload.php'; $http = Network::Create('curl');//指定使用curl組件 $http->open('POST', $url); $http->enableGzip();//打開gzpi $http->setTimeOuts(120, 120, 0, 0);//設(shè)置超時(shí) $http->addBinary('file', 'D:/www/web.config');//指定某個(gè)文件 $http->send(); echo $http->responseText; //備注一下,1.7.2以下版本的filegetcontents和fsockopen,提交到自簽證書的https網(wǎng)站有故障,1.7.2已修復(fù)了。
示例代碼 6:Network__curl 的特別用法:
$http = Network::Create('curl');//指定使用curl組件 $http->open('GET', 'https://www.baidu.com/'); //眾所周知curl有很多種設(shè)置,NetWork暴露出一個(gè)curl對象可供設(shè)置使用,需要在open()后send()之前。 if(is_object($http->ch)) { curl_setopt($http->ch, CURLOPT_URL, 'https://www.so.com/s'); } $http->send();