如何通过代理使用 CURL?
我希望将curl 设置为使用代理服务器。 url 由 html 表单提供,这不是问题。没有代理它工作正常。我在这个网站和其他网站上找到了代码,但它们不起作用。任何帮助找到正确解决方案的帮助将不胜感激。我觉得波纹管很接近,但我错过了一些东西。谢谢。
我从这里改编的以下代码 http://www.webmasterworld.com/forum88/10572.htm< /a> 但它返回有关第 12 行缺少 T_VARIABLE 的错误消息。
<?
$url = '$_POST[1]';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '66.96.200.39:80');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1)
curl_exec ($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
echo '<br />';
print_r($curl_info);
?>
以下内容来自 curl through proxy returns no content
<?
$proxy = "66.96.200.39:80";
$proxy = explode(':', $proxy);
$url = "$_POST[1]";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_HEADER, 1);
$exec = curl_exec($ch);
echo curl_error($ch);
print_r(curl_getinfo($ch));
echo $exec;
?>
目前在 pelican-cement.com 上可用,但也不起作用。
更新: 感谢大家的帮助,我做了以上修改。现在它只返回一个空白屏幕。
<?
$url = $_POST['1'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '66.96.200.39:80');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_exec ($ch);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
?>
I am looking to set curl to use a proxy server. The url is provided by an html form, which has not been a problem. Without the proxy it works fine. I have found code on this and other sites, but they do not work. Any help in finding the correct solution would be much appreciated. I feel that the bellow are close, but that I am missing something. Thank You.
The bellow code I adapted from here http://www.webmasterworld.com/forum88/10572.htm but it returns an error message about a missing T_VARIABLE on line 12.
<?
$url = '$_POST[1]';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '66.96.200.39:80');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1)
curl_exec ($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
echo '<br />';
print_r($curl_info);
?>
The bellow is from curl through proxy returns no content
<?
$proxy = "66.96.200.39:80";
$proxy = explode(':', $proxy);
$url = "$_POST[1]";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_HEADER, 1);
$exec = curl_exec($ch);
echo curl_error($ch);
print_r(curl_getinfo($ch));
echo $exec;
?>
is currently live on pelican-cement.com but also does not work.
UPDATE:
Thank you for all your help, I made the above changes. Now it only returns a blank screen.
<?
$url = $_POST['1'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '66.96.200.39:80');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_exec ($ch);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个已删除错误的工作版本。
我添加了
CURLOPT_PROXYUSERPWD
以防您的任何代理需要用户名和密码。我将
CURLOPT_RETURNTRANSFER
设置为 1,以便数据将返回到$curl_scraped_page
变量。我删除了第二个额外的
curl_exec($ch);
,这将阻止返回变量。我将您的代理 IP 和端口合并到一项设置中。
我还删除了
CURLOPT_HTTPPROXYTUNNEL
和CURLOPT_CUSTOMREQUEST
因为它是默认值。如果您不希望返回标头,请注释掉
CURLOPT_HEADER
。要禁用代理,只需将其设置为空即可。
如有任何问题,请随时提出,我每天都与
cURL
一起工作。Here is a working version with your bugs removed.
I have added
CURLOPT_PROXYUSERPWD
in case any of your proxies require a user name and password.I set
CURLOPT_RETURNTRANSFER
to 1, so that the data will be returned to$curl_scraped_page
variable.I removed a second extra
curl_exec($ch);
which would stop the variable being returned.I consolidated your proxy IP and port into one setting.
I also removed
CURLOPT_HTTPPROXYTUNNEL
andCURLOPT_CUSTOMREQUEST
as it was the default.If you don't want the headers returned, comment out
CURLOPT_HEADER
.To disable the proxy simply set it to null.
Any questions feel free to ask, I work with
cURL
every day.我已经解释了 CURL PROXY 所需的各种 CURL 选项的使用。
I have explained use of various CURL options required for CURL PROXY.
在 php 脚本文件中声明代理设置的问题已得到修复。
Post declaring the proxy settings in the php script file issue has been fixed.
这是一个经过良好测试的函数,我在我的项目中使用了它,并带有详细的自解释注释
很多时候,除 80 之外的端口被服务器防火墙阻止,因此代码似乎在本地主机上运行良好,但在服务器上却运行不佳
Here is a well tested function which i used for my projects with detailed self explanatory comments
There are many times when the ports other than 80 are blocked by server firewall so the code appears to be working fine on localhost but not on the server