本來都能夠正常抓取
但前幾天開始,變的時好時壞
有時正常運行,有時跑了很久都跑不出來
但用瀏覽器訪問都正常
狀況網址之一: http://report.penghu.gov.tw/OpenFront/report/report_detail.jsp?sysId=C105AQ022
想請問可能是哪方面的原因
謝謝
我測試一下,沒看到問題...另外,curl_getinfo()要在curl_exec()之後執行啦,沒真的去連不會取得資訊阿。
簡單的測試:
<?php
$url = 'http://report.penghu.gov.tw/OpenFront/report/report_detail.jsp?sysId=C105AQ022';
$h = curl_init($url);
curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
$c = curl_exec($h);
$i = curl_getinfo($h);
curl_close($h);
echo nl2br(print_r($i, true));
結果看起來算正常:
Array
(
[url] => http://report.penghu.gov.tw/OpenFront/report/report_detail.jsp?sysId=C105AQ022
[content_type] => text/html;charset=UTF-8
[http_code] => 200
[header_size] => 248
[request_size] => 109
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.246052
[namelookup_time] => 0.004252
[connect_time] => 0.095965
[pretransfer_time] => 0.096012
[size_upload] => 0
[size_download] => 12032
[speed_download] => 48900
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0.184224
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 163.29.98.42
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => 172.20.10.2
[local_port] => 50278
)
curl_getinfo 只有在成功訪問目標網址時才會有辦法取得正確的資料,
所以正確作法是:
$c = curl_exec($h);
$i = curl_getinfo($h);
if($c === false){
$i['curl_error'] = curl_error(); // 你可以另外設個變數儲存他,我只是單純懶惰而已。
}
var_dump($curl);