如何通过Proxy连接使用cURL获取数据
我想使用代理连接在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这可以帮助 http://code.google.com/apis/gdata /articles/using_cURL.html#authentication
您应该真正了解您复制的内容 http://www.fromzerotoseo.com/ scraping-websites-php-curl-proxy/
您可以使用
,但您必须解析结果(也许使用 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
but you have do parse the results (maybe using SimpleXMLElement)...good luck