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

发布于 2024-12-10 11:35:46 字数 1832 浏览 0 评论 0原文

我想使用代理连接在 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! ) 

I would like to open any url with help of cURL using proxy connection. Below are the codes... still we are not able to connect this and getting positive data through this function Please provide the suitable help to resolve this issue. Every time i run this code. I got the "Else" conditioned message.

<?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" ;
}
?>

When we print this "print_r($result);" the result would be ..........

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2024-12-17 11:35:46

也许这可以帮助 http://code.google.com/apis/gdata /articles/using_cURL.html#authentication
您应该真正了解您复制的内容 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)...祝您好运

Maybe this can help http://code.google.com/apis/gdata/articles/using_cURL.html#authenticating
You should really understand what you have copied http://www.fromzerotoseo.com/scraping-websites-php-curl-proxy/

you can use

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);

}

but you have do parse the results (maybe using SimpleXMLElement)...good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文