Re: [請益] 如何curl這網站資訊呢?

作者: shadowjohn (轉角遇到愛)   2015-10-06 22:54:18
※ 引述《jami520 (我的生命因你而發光)》之銘言:
: 單純點選網址可以正常看到資訊 http://goo.gl/v9Yr5r
: 但使用curl並沒有任何數據顯示,不曉得是哪邊的問題呢? 謝謝
: $c = curl_init();
: curl_setopt($c, CURLOPT_URL,
: "http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_3008.tw|tse_2330.
: tw&json=1&delay=0&d=20151006");
: curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
: $timeout = 15;
: curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $timeout);
: $data = curl_exec($c);
: curl_close($c)
: echo $data;
試了半天試出來了~
<?php
$ch = curl_init();
$url =
"http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_3008.tw|tse_2330.tw&json=1&delay=0&d=20151006";
//網址的最後建議可以加上 &_t=".time()
//短時間連送三次同一個網址會停掉
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1, // 連1都能work就代表你成功了
CURLOPT_VERBOSE => 0
));
//以下 header 必需照抄,亂來會失敗,參考自 firefox (f12檔頭)
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: mis.twse.com.tw',
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101
Firefox/41.0',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',
'Cookie: JSESSIONID=DA7F410960A1BDFBE36947007403F24B;
JSESSIONID=6E6AD946C7DA0BAEE4AF3FC9BBB15B2C',
'Connection: keep-alive',
'Cache-Control: max-age=0'
));
$response = curl_exec($ch);
//curl_close($ch);
echo gzdecode( $response);
//稍微有點難度,主要應該還是在header的內容
//curl 預設的順序還是內容總之就是不行,此內容直接照抄 firefox 裡
//的,就正常可以過
//最後取到的是 gzip 的 binary ,利用 gzdecode 反解可得 json
//比較需要調整的部分也許是 Cookie的內容,也許要
//大量抓取時,先把CURLOPT_FOLLOWLOCATION 設成 0,不要被302卡頁
//偷到第一次cookie 後,之後就用偷來的 cookie 瀏覽
//上面範例是寫死的,點出來即可 XD
以下是實作
[root@3wa tw]# php t.php
{"msgArray":[{"ts":"0","fv":"2","tk0":"3008.tw_tse_20151006_B_9999200852","tk1":"3008.tw_tse_20151006_B_9999180800","oa":"2675.00","ob":"2670.00","tlong":"1444113000000","ot":"14:30:00","f":"9_11_5_24_37_","ex":"tse","g":"5_2_12_45_15_","ov":"4811","d":"20151006","it":"12","b":"2690.00_2685.00_2680.00_2670.00_2665.00_","c":"3008","mt":"000000","a":"2695.00_2700.00_2705.00_2710.00_2715.00_","n":"
大立光
","o":"2580.00","l":"2580.00","oz":"2670.00","h":"2715.00","ip":"0","i":"26","w":"2235.00","v":"2226","u":"2725.00","t":"13:30:00","s":"111","pz":"2690.00","tv":"111","p":"0","nf":"
大立光電股份有限公司
","ch":"3008.tw","z":"2690.00","y":"2480.00","ps":"109"},{"ts":"0","fv":"15","tk0":"2330.tw_tse_20151006_B_9999200681","tk1":"2330.tw_tse_20151006_B_9999180890","oa":"133.00","ob":"132.50","tlong":"1444113000000","ot":"14:30:00","f":"899_1413_2172_2113_2051_","ex":"tse","g":"1476_4939_3254_3619_267_","ov":"13004","d":"20151006","it":"12","b":"132.50_132.00_131.50_131.00_130.50_","c":"2330","mt":"000000","a":"133.00_133.50_134.00_134.50_135.00_","n":"
台積電
","o":"135.00","l":"132.50","oz":"132.50","h":"135.50","ip":"0","i":"24","w":"119.50","v":"48613","u":"145.50","t":"13:30:00","s":"3859","pz":"133.00","tv":"3859","p":"0","nf":"
台灣積體電路製造股份有限公司
","ch":"2330.tw","z":"133.00","y":"132.50","ps":"3728"}],"userDelay":5000,"rtmessage":"OK","referer":"","queryTime":{"sysTime":"22:55:04","sessionLatestTime":1444113000000,"sysDate":"20151006","sessionKey":"tse_3008.tw_20151006|tse_2330.tw_20151006|","sessionFromTime":1444113000000,"stockInfoItem":1119,"showChart":false,"sessionStr":"UserSession","stockInfo":119245},"rtcode":"0000"}[root@3wa
tw]#
作者: jami520 (我的生命因你而發光)   2015-10-07 07:20:00
大大真是太厲害了,小弟初學只會按照網路寫法套用,試了好幾天,也都找不出是哪邊問題,實在非常感謝!
作者: shadowjohn (轉角遇到愛)   2015-10-07 09:42:00
沒事要多翻翻書~坊間還是有不錯的讀物有時搜尋到的只是一堆基本的,反而要花很多時間過濾
作者: tkdmaf (皮皮快跑)   2015-10-07 09:56:00
我偷偷跑去玩你的色塊遊戲,連續硬幹50題!^^shadowjohn
作者: crossdunk (推噓自如)   2015-10-07 10:26:00
五十題 好猛
作者: shadowjohn (轉角遇到愛)   2015-10-07 10:52:00
:D 有點會啊~

Links booklink

Contact Us: admin [ a t ] ucptt.com