0
我想使用代理連接在cURL的幫助下打開任何URL。以下是代碼...我們仍然無法連接並通過此功能獲取正面數據請提供適當的幫助來解決此問題。每次我運行這個代碼。我收到了「Else」條件消息。如何通過代理連接使用cURL獲取數據
<?php
function getPage($proxy, $url, $referer, $agent, $header, $timeout) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$result['EXE'] = curl_exec($ch);
$result['INF'] = curl_getinfo($ch);
$result['ERR'] = curl_error($ch);
curl_close($ch);
return $result;
}
?>
<?php
$result = getPage(
'[64.71.138.122]:[80]', // use valid proxy
'http://www.google.com/search?q=twitter',
'http://www.google.com/',
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
1,
5);
if (empty($result['ERR'])) {
echo "Positive message" ;
} else {
echo "Negative Message" ;
}
?>
當我們打印這個「print_r($ result);」其結果將是..........
Array ([EXE] => [INF] => Array ([url] => http://www.google.com/search?q=twitter [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 5.019769 [namelookup_time] => 0.000129 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0) [ERR] => connect() timed out!)
如果你echo $ result ['ERR'];`,它說什麼? – DaveRandom 2011-10-17 12:07:23
另外,我很確定`[64.71.138.122]:[80]`應該只說`64.71.138.122`(儘管我可能會錯這個) – DaveRandom 2011-10-17 12:09:25