如何通过代理使用 CURL? [英] How to use CURL via a proxy?
问题描述
我希望将 curl 设置为使用代理服务器.url是html表单提供的,一直没有问题.没有代理它工作正常.我在这个网站和其他网站上找到了代码,但它们不起作用.任何帮助找到正确的解决方案将不胜感激.我觉得波纹管很近,但我错过了一些东西.谢谢.
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.
我从这里改编的波纹管代码http://www.webmasterworld.com/forum88/10572.htm 但它返回关于第 12 行缺少 T_VARIABLE 的错误消息.
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);
?>
以下内容来自curl through proxy 不返回任何内容
<?
$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 上可用,但也无法使用.
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;
?>
推荐答案
这是一个删除了错误的工作版本.
Here is a working version with your bugs removed.
$url = 'http://dynupdate.no-ip.com/ip.php';
$proxy = '127.0.0.1:8888';
//$proxyauth = 'user:password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
我添加了 CURLOPT_PROXYUSERPWD
以防您的任何代理需要用户名和密码.我将 CURLOPT_RETURNTRANSFER
设置为 1,以便将数据返回到 $curl_scraped_page
变量.
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.
我删除了第二个额外的 curl_exec($ch);
这将停止返回变量.我将您的代理 IP 和端口合并为一个设置.
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.
我还删除了 CURLOPT_HTTPPROXYTUNNEL
和 CURLOPT_CUSTOMREQUEST
,因为它们是默认设置.
I also removed CURLOPT_HTTPPROXYTUNNEL
and CURLOPT_CUSTOMREQUEST
as it was the default.
如果您不想返回标头,请注释掉 CURLOPT_HEADER
.
If you don't want the headers returned, comment out CURLOPT_HEADER
.
要禁用代理,只需将其设置为 null.
To disable the proxy simply set it to null.
curl_setopt($ch, CURLOPT_PROXY, null);
任何问题都可以问,我每天都在使用 cURL
.
Any questions feel free to ask, I work with cURL
every day.
这篇关于如何通过代理使用 CURL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!