| 14 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
#指派命名空間
|
|
|
4 |
namespace qbpwcf;
|
|
|
5 |
|
|
|
6 |
#匯入外部套件
|
|
|
7 |
include("../allInOne.php");
|
|
|
8 |
|
|
|
9 |
#提示目前正在測試的部分
|
|
|
10 |
echo"<hr>catchWebContent::wget - testCase 1<p>";
|
|
|
11 |
|
|
|
12 |
/*
|
|
|
13 |
|
|
|
14 |
#涵式說明:
|
|
|
15 |
#設定檢視該網頁時預設的編碼
|
|
|
16 |
#必填的參數:
|
|
|
17 |
#$conf["encodingName"]="big5";#為網頁顯示的編碼的名稱.
|
|
|
18 |
#例如:"utf-8","big5"
|
|
|
19 |
#header::htmlEncoding($conf);
|
|
|
20 |
#unset($conf);
|
|
|
21 |
|
|
|
22 |
#涵式說明:
|
|
|
23 |
#抓取的特定網頁的某段內容
|
|
|
24 |
#回傳的結果:
|
|
|
25 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
26 |
#$result["error"],錯誤訊息陣列.
|
|
|
27 |
#$result["webContent"]["lineCount"],爲總共有機行原始碼。
|
|
|
28 |
#$result["webContent"]["full"],爲抓取的全部網頁程式碼
|
|
|
29 |
#$result["webContent"][i],爲第i+1行的程式碼
|
|
|
30 |
#必填的參數:
|
|
|
31 |
$conf["loadingPotision"]="http://www.nmp.gov.tw/news/list.php";#要抓取的網頁網址
|
|
|
32 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
33 |
$conf["fileArgu"]=__FILE__;
|
|
|
34 |
#可省略的參數:
|
|
|
35 |
#$conf["startLineNum"]="233";#從第幾行開始擷取,預設爲從第1行(第0個元素)。
|
|
|
36 |
#$conf["endLineNum"]="273";#到第幾行結束截取,預設爲從最後一行(最後一個元素)。
|
|
|
37 |
#$conf["processString"]="true";#爲是否要變更特定字串內容,預設爲false,不做變更,若設爲"true"則爲要進行處理字串的功能。
|
|
|
38 |
#$conf["selectedCharacter"]=array("width='90%'","width='80%'","width='20%'","nmpnews.php?","<a","border='0'");#爲被選擇要處理的字串/字元,須爲陣列值。
|
|
|
39 |
#若不設定則預設爲要將這些字串作替換(" ","href","iframe","<",">","<a","/>",";","/","=","#",".","'","$","%","&","|")。
|
|
|
40 |
#$conf["changeTo"]=array("","","","http://www.nmp.gov.tw/nmpnews.php?","<a target = _blank ","border=1px");#爲被選擇的字元要換成什麼字串/字元,須爲陣列值。若不設定,則預設爲更換成""(空字串)。
|
|
|
41 |
#$conf["changeCharSet"],字元陣列,轉換全文編碼,預設不使用,$conf["changeCharSet"][0]為原始編碼,$conf["changeCharSet"][0]為新的編碼.
|
|
|
42 |
#$conf["changeCharSet"]=array("big5","utf-8");
|
|
|
43 |
$wget=catchWebContent::wget($conf);
|
|
|
44 |
unset($conf);
|
|
|
45 |
|
|
|
46 |
var_dump($wget);
|
|
|
47 |
|
|
|
48 |
*/
|
|
|
49 |
|
|
|
50 |
#提示目前正在測試的部分
|
|
|
51 |
echo"<hr>catchWebContent::wget - testCase 2<p>";
|
|
|
52 |
|
|
|
53 |
/*
|
|
|
54 |
|
|
|
55 |
#涵式說明:
|
|
|
56 |
#抓取的特定網頁的某段內容
|
|
|
57 |
#回傳的結果:
|
|
|
58 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
59 |
#$result["error"],錯誤訊息陣列.
|
|
|
60 |
#$result["function"],函數名稱.
|
|
|
61 |
#$result["oriWebContent"]["full"],原始檔案的完整內容.
|
|
|
62 |
#$result["oriWebContent"]["byLine"],用陣列儲存的原始檔案逐行內容.
|
|
|
63 |
#$result["oriWebContent"]["lineCount"],原始檔案的總共行數.
|
|
|
64 |
#$result["webContent"]["lineCount"],爲總共有機行原始碼。
|
|
|
65 |
#$result["webContent"]["full"],為檔案的完整內容,爲抓取的全部網頁程式碼
|
|
|
66 |
#$result["webContent"][i],爲第i+1行的程式碼
|
|
|
67 |
#必填的參數:
|
|
|
68 |
$conf["loadingPotision"]="https://tw.yahoo.com/";#要抓取的網頁網址
|
|
|
69 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
70 |
$conf["fileArgu"]=__FILE__;
|
|
|
71 |
#可省略的參數:
|
|
|
72 |
#$conf["startLineNum"]="";#從第幾行開始擷取,預設爲從第1行(第0個元素)。
|
|
|
73 |
#$conf["endLineNum"]="";#到第幾行結束截取,預設爲從最後一行(最後一個元素)。
|
|
|
74 |
#$conf["processString"]="";#爲是否要變更特定字串內容,預設爲false,不做變更,若設爲"true"則爲要進行處理字串的功能。
|
|
|
75 |
#$conf["selectedCharacter"]=array("");#爲被選擇要處理的字串/字元,須爲陣列值。
|
|
|
76 |
#若不設定則預設爲要將這些字串作替換(" ","href","iframe","<",">","<a","/>",";","/","=","#",".","'","$","%","&","|")。
|
|
|
77 |
#$conf["changeTo"]="";#爲被選擇的字元要換成什麼字串/字元,須爲陣列值。若不設定,則預設爲更換成""(空字串)。
|
|
|
78 |
var_dump(catchWebContent::wget($conf));
|
|
|
79 |
unset($conf);
|
|
|
80 |
|
|
|
81 |
*/
|
|
|
82 |
|
|
|
83 |
#提示目前正在測試的部分
|
|
|
84 |
echo"<hr>catchWebContent::toHtml - testCase 1<p>";
|
|
|
85 |
|
|
|
86 |
/*
|
|
|
87 |
|
|
|
88 |
#函數說明:
|
|
|
89 |
#將特定網址的網頁內容儲存成html檔案
|
|
|
90 |
#回傳結果:
|
|
|
91 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
92 |
#$result["error"],錯誤訊息陣列.
|
|
|
93 |
#$result["function"],當前執行的函式名稱.
|
|
|
94 |
#$result["html"],取得的網頁檔位置.
|
|
|
95 |
#必填參數:
|
|
|
96 |
#$conf["saveLocation"],字串,下載好的網頁要儲存到哪邊
|
|
|
97 |
$conf["saveLocation"]="catchWebContentT/catchWebContent::toHtml";
|
|
|
98 |
#$conf["webPageLocation"],字串,要下載的網頁目錄位置與檔案名稱.ex:「function/about.php」
|
|
|
99 |
$conf["webPageLocation"]="~qbpwcf/";
|
|
|
100 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑.
|
|
|
101 |
$conf["fileArgu"]=__FILE__;
|
|
|
102 |
#可省略參數:
|
|
|
103 |
#$conf["type"],字串,網頁的協定,預設為"http",可以用的還有"https".
|
|
|
104 |
$conf["type"]="https";
|
|
|
105 |
#$conf["webHostAddressOrDNS"],字串,網頁所處的IP或DNS,預設為"127.0.0.1".
|
|
|
106 |
$conf["webHostAddressOrDNS"]="127.0.0.1";
|
|
|
107 |
#$conf["port"],字串,要使用的port,預設"80",一般來說http為80,https為443.
|
|
|
108 |
$conf["port"]="443";
|
|
|
109 |
var_dump(catchWebContent::toHtml($conf));
|
|
|
110 |
unset($conf);
|
|
|
111 |
|
|
|
112 |
*/
|
|
|
113 |
|
|
|
114 |
#提示目前正在測試的部分
|
|
|
115 |
echo"<hr>catchWebContent::getFullWeb - testCase 1<p>";
|
|
|
116 |
|
|
|
117 |
/*
|
|
|
118 |
|
|
|
119 |
#涵式說明:
|
|
|
120 |
#抓取的特定網頁的某段內容
|
|
|
121 |
#回傳的結果:
|
|
|
122 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
123 |
#$result["error"],錯誤訊息陣列.
|
|
|
124 |
#$result["function"],函數名稱.
|
|
|
125 |
#$result["webContent"]["lineCount"],爲總共有機行原始碼。
|
|
|
126 |
#$result["webContent"]["full"],爲抓取的全部網頁程式碼
|
|
|
127 |
#$result["webContent"][i],爲第i+1行的程式碼
|
|
|
128 |
#必填的參數:
|
|
|
129 |
#$conf["loadingPotision"],字串,要抓取的網頁網址
|
|
|
130 |
$conf["loadingPotision"]="https://start.fedoraproject.org/";#要抓取的網頁網址
|
|
|
131 |
#可省略的參數:
|
|
|
132 |
#$conf["startLineNum"],從第幾行開始擷取,預設爲從第1行(第0個元素)。
|
|
|
133 |
#$conf["startLineNum"]="";
|
|
|
134 |
#$conf["endLineNum"],到第幾行結束截取,預設爲從最後一行(最後一個元素)。
|
|
|
135 |
#$conf["endLineNum"]="";
|
|
|
136 |
#$conf["processString"],爲是否要變更特定字串內容,預設爲false,不做變更,若設爲"true"則爲要進行處理字串的功能。
|
|
|
137 |
#$conf["processString"]="";
|
|
|
138 |
#$conf["selectedCharacter"],d爲被選擇要處理的字串/字元,須爲陣列值。
|
|
|
139 |
#$conf["selectedCharacter"]=array("");
|
|
|
140 |
#若不設定則預設爲要將這些字串作替換(" ","href","iframe","<",">","<a","/>",";","/","=","#",".","'","$","%","&","|")。
|
|
|
141 |
#$conf["changeTo"],爲被選擇的字元要換成什麼字串/字元,須爲陣列值。若不設定,則預設爲更換成""(空字串)。
|
|
|
142 |
#$conf["changeTo"]=array("");
|
|
|
143 |
#$conf["changeCharSet"],字元陣列,轉換全文編碼,預設不使用,$conf["changeCharSet"][0]為原始編碼,$conf["changeCharSet"][0]為新的編碼.
|
|
|
144 |
#$conf["changeCharSet"]=array("big5","utf-8");
|
|
|
145 |
#備註:
|
|
|
146 |
#$conf["changeCharSet"]參數沒有效果.
|
|
|
147 |
#有些網站會無法透過此方法取得網站內容.
|
|
|
148 |
#參考資料來源:
|
|
|
149 |
#iconv用法 => http://php.net/manual/en/function.iconv.php
|
|
|
150 |
var_dump(catchWebContent::getFullWeb($conf));
|
|
|
151 |
unset($conf);
|
|
|
152 |
|
|
|
153 |
*/
|
|
|
154 |
|
|
|
155 |
#說明要測試的案例
|
|
|
156 |
echo"<hr>catchWebContent::nativeMulti - testCase 1<br>";
|
|
|
157 |
|
|
|
158 |
/*
|
|
|
159 |
|
|
|
160 |
#函數說明:
|
|
|
161 |
#php內建的curl方法
|
|
|
162 |
#回傳結果:
|
|
|
163 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
164 |
#$result["error"],錯誤訊息.
|
|
|
165 |
#$result["function"],當前執行的函數名稱.
|
|
|
166 |
#$result["content"][$i]["info"],第$i+1個要求的回應資訊.
|
|
|
167 |
#$result["content"][$i]["res"],第$i+1個要求的回應.
|
|
|
168 |
#必填參數:
|
|
|
169 |
#$conf["url"],字串陣列,有哪些網址要拜訪.
|
|
|
170 |
$conf["url"]=array("https://tw.yahoo.com","https://www.google.com.tw","https://start.fedoraproject.org/","https://127.0.0.1/~qbpwcf");
|
|
|
171 |
#可省略的參數:
|
|
|
172 |
#$conf["opts"],一維字串陣列,要設定的curl連線參數,元素的key為參數的名稱,切記要為long形態(沒有""框住的文字),元素的value為參數的數值內容.
|
|
|
173 |
#$conf["opts"];
|
|
|
174 |
#參考資料:
|
|
|
175 |
#curl_multi_ini(多執行緒執行curl)=>http://php.net/manual/en/function.curl-multi-init.php
|
|
|
176 |
#備註:
|
|
|
177 |
#建構中...
|
|
|
178 |
$native=catchWebContent::nativeMulti($conf);
|
|
|
179 |
unset($conf);
|
|
|
180 |
|
|
|
181 |
#如果執行失敗
|
|
|
182 |
if($native["status"]==="false"){
|
|
|
183 |
|
|
|
184 |
#印出訊息
|
|
|
185 |
var_dump($native);
|
|
|
186 |
|
|
|
187 |
#終止程式
|
|
|
188 |
exit;
|
|
|
189 |
|
|
|
190 |
}#if end
|
|
|
191 |
|
|
|
192 |
#印出取得的資訊
|
|
|
193 |
var_dump($native);
|
|
|
194 |
|
|
|
195 |
*/
|
|
|
196 |
|
|
|
197 |
/*
|
|
|
198 |
|
|
|
199 |
#說明要測試的案例
|
|
|
200 |
echo"<hr>catchWebContent::curlCmd - testCase 1<br>";
|
|
|
201 |
|
|
|
202 |
#函式說明:
|
|
|
203 |
#運行curl cmd
|
|
|
204 |
#回傳結果:
|
|
|
205 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
206 |
#$result["error"],錯誤訊息陣列.
|
|
|
207 |
#$result["function"],當前執行的函式名稱.
|
|
|
208 |
#$result["content"],取得的回應內容.
|
|
|
209 |
#必填參數:
|
|
|
210 |
#$conf["url"],字串,目標url.
|
|
|
211 |
$conf["url"]="https://www.google.com";
|
|
|
212 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
213 |
$conf["fileArgu"]=__FILE__;
|
|
|
214 |
#可省略參數:
|
|
|
215 |
#$conf["header"],字串陣列,要傳送的header.
|
|
|
216 |
$conf["header"]=array("lib:qbpwcf");
|
|
|
217 |
#$conf["allowAnySSLcertificate"],字串,是否允許不可信任的SSL憑證.
|
|
|
218 |
#$conf["allowAnySSLcertificate"]="";
|
|
|
219 |
#$conf["postVar"],字串陣列,每個要傳送的post變數名稱(陣列的key值)與數值.
|
|
|
220 |
$conf["postVar"]=array("a"=>"b","c"=>"d");
|
|
|
221 |
#備註:
|
|
|
222 |
#僅能在命令列下執行.
|
|
|
223 |
$curlCmd=catchWebContent::curlCmd($conf);
|
|
|
224 |
unset($conf);
|
|
|
225 |
|
|
|
226 |
#如果執行失敗
|
|
|
227 |
if($curlCmd["status"]==="false"){
|
|
|
228 |
|
|
|
229 |
#印出訊息
|
|
|
230 |
var_dump($curlCmd);
|
|
|
231 |
|
|
|
232 |
#終止程式
|
|
|
233 |
exit;
|
|
|
234 |
|
|
|
235 |
}#if end
|
|
|
236 |
|
|
|
237 |
#印出結果
|
|
|
238 |
var_dump($curlCmd);
|
|
|
239 |
|
|
|
240 |
*/
|
|
|
241 |
|
|
|
242 |
?>
|