http://fuzhou.8684.cn/
My Code是這樣。
'*/*', 'Accept-Charset' => 'UTF-8,*;q=0.5', 'Accept-Encoding' => 'gzip,deflate,sdch', 'Accept-Language' => 'zh-CN,zh;q=0.8', 'Connection' => 'keep-alive', 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer' => 'http://fuzhou.8684.cn/', 'User-Agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11', 'X-Requested-With' => 'XMLHttpRequest',));$dat = curl_exec($ch);var_dump(curl_error($ch));//curl_close($ch);var_dump($dat);//var_dump(curl_error($ch));
可是輸出的是
string(23) "Empty reply from server" bool(false)
回複內容:
http://fuzhou.8684.cn/
My Code是這樣。
'*/*', 'Accept-Charset' => 'UTF-8,*;q=0.5', 'Accept-Encoding' => 'gzip,deflate,sdch', 'Accept-Language' => 'zh-CN,zh;q=0.8', 'Connection' => 'keep-alive', 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer' => 'http://fuzhou.8684.cn/', 'User-Agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11', 'X-Requested-With' => 'XMLHttpRequest',));$dat = curl_exec($ch);var_dump(curl_error($ch));//curl_close($ch);var_dump($dat);//var_dump(curl_error($ch));
可是輸出的是
string(23) "Empty reply from server" bool(false)
我只能說你的curl手冊沒有看認真,CURLOPT_HTTPHEADER這個選項的用法肯定是按你自己想象的來的,實際上並不是這樣,應該
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: */*', 'Accept-Charset: UTF-8,*;q=0.5', 'Accept-Encoding: gzip,deflate,sdch', 'Accept-Language: zh-CN,zh;q=0.8', 'Connection: keep-alive', 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8', 'Referer: http://fuzhou.8684.cn/', 'User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11', 'X-Requested-With: XMLHttpRequest',));
所以說得認真看手冊
補充一下。本來打算用curl+正則 採集8684的資料。
後來發現 愛幫網有免費的公交資料API。
http://www.aibang.com/api/
查了下PHP手冊。
樓主將代碼:
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept' => '*/*', 'Accept-Charset' => 'UTF-8,*;q=0.5', 'Accept-Encoding' => 'gzip,deflate,sdch', 'Accept-Language' => 'zh-CN,zh;q=0.8', 'Connection' => 'keep-alive', 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer' => 'http://fuzhou.8684.cn/', 'User-Agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11', 'X-Requested-With' => 'XMLHttpRequest',));
調用的函數 curl_setopt 替換為 curl_setopt_array 試試。
Curl確實功能很強大!以前一直使用fsocket來在程式中發送http請求,後來試用curl後,感覺方便以及靈活得多!
收藏了一篇關於Curl的博文,雖然簡單,但是感覺不錯的!
http://www.360weboy.com/web-service/c...