2011-10-17 68 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)...祝你好运

相关问题