2011-10-17 97 views
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!) 
+0

如果你echo $ result ['ERR'];`,它說什麼? – DaveRandom 2011-10-17 12:07:23

+1

另外,我很確定`[64.71.138.122]:[80]`應該只說`64.71.138.122`(儘管我可能會錯這個) – DaveRandom 2011-10-17 12:09:25

回答

1

也許這可以幫助http://code.google.com/apis/gdata/articles/using_cURL.html#authenticating 你真的應該明白你已拷貝http://www.fromzerotoseo.com/scraping-websites-php-curl-proxy/

您可以用什麼

function curl_google($keyword){ 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 
'http://www.google.com/search?hl=en&q='.urlencode($keyword).'&btnG=Google+Search&meta='); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FILETIME, true); 
return $data = curl_exec($ch); 
curl_close($ch); 

} 

但你有解析結果(也許使用SimpleXMLElement)...祝你好運

相關問題