如何使用cURL通过代理连接获取数据

时间:2011-10-17 12:02:36

标签: php curl proxy

我想使用代理连接在cURL的帮助下打开任何网址。以下是代码......我们仍然无法通过此功能连接此数据并获取正数据请提供合适的帮助来解决此问题。每次我运行此代码。我得到了“Else”条件消息。

<?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! ) 

1 个答案:

答案 0 :(得分: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)...祝你好运