Php 如何通过代理使用CURL?

Php 如何通过代理使用CURL?,php,curl,proxy,php-curl,Php,Curl,Proxy,Php Curl,我希望将curl设置为使用代理服务器。url由html表单提供,这不是问题。如果没有代理,它可以正常工作。我在这个网站和其他网站上找到了代码,但它们不起作用。如能帮助您找到正确的解决方案,我们将不胜感激。我觉得怒吼声很近,但我错过了一些东西。多谢各位 下面的代码是我从这里改编的,但它在第12行返回了一条关于缺少T_变量的错误消息 <? $url = '$_POST[1]'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); cu

我希望将curl设置为使用代理服务器。url由html表单提供,这不是问题。如果没有代理,它可以正常工作。我在这个网站和其他网站上找到了代码,但它们不起作用。如能帮助您找到正确的解决方案,我们将不胜感激。我觉得怒吼声很近,但我错过了一些东西。多谢各位

下面的代码是我从这里改编的,但它在第12行返回了一条关于缺少T_变量的错误消息

<?

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

吼声来自


目前位于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;
?> 

这是一个已删除bug的工作版本

$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\u RETURNTRANSFER
设置为1,这样数据将返回到
$curl\u scraped\u page
变量

我删除了第二个额外的
curl\u exec($ch)
将停止返回变量。 我将您的代理IP和端口整合到一个设置中

我还删除了
CURLOPT_HTTPPROXYTUNNEL
CURLOPT_CUSTOMREQUEST
,因为这是默认设置

如果不希望返回标题,请注释掉
CURLOPT_HEADER

要禁用代理,只需将其设置为null

curl_setopt($ch, CURLOPT_PROXY, null);

任何问题都可以随意提问,我每天都使用
cURL

这是一个经过良好测试的函数,我在我的项目中使用了它,并提供了详细的自我解释性注释


很多时候,服务器防火墙会阻止80以外的端口,因此代码在本地主机上运行正常,但在服务器上运行不正常

function get_page($url){

global $proxy;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HEADER, 0); // return headers 0 no 1 yes
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return page 1:yes
curl_setopt($ch, CURLOPT_TIMEOUT, 200); // http request timeout 20 seconds
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the url changes
curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //if http server gives redirection responce
curl_setopt($ch, CURLOPT_USERAGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); // cookies storage / here the changes have been made
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // the page encoding

$data = curl_exec($ch); // execute the http request
curl_close($ch); // close the connection
return $data;
}

我已经解释了CURL代理所需的各种CURL选项的使用

$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);         // URL for CURL call
curl_setopt($ch, CURLOPT_PROXY, $proxy);     // PROXY details with port
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);   // Use if proxy have username and password
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); // If expected to call with specific PROXY type
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  // If url has redirects then go to the final redirected URL.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);  // Do not outputting it out directly on screen.
curl_setopt($ch, CURLOPT_HEADER, 1);   // If you want Header information of response else make 0
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;
root@APPLICATIOSERVER:/var/www/html#php connectiontest.php 61e23468-949e-4103-8e08-9db09249e8s1 OpenSSL SSL_connect:SSL_ERROR_SYSCALL连接到10.172.123.1:80root@APPLICATIOSERVER:/var/www/html# 在php脚本文件中声明代理设置的问题已得到修复

$proxy = '10.172.123.1:80'; curl_setopt($cSession, CURLOPT_PROXY, $proxy); // PROXY details with port $proxy='10.172.123.1:80'; curl_setopt($cSession,CURLOPT_PROXY,$PROXY);//带有端口的代理详细信息
第12行缺少分号。另外,您需要将$url='$\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ pelican-cement.com上的表单有名为“firstname”和“lastname”的输入,但是没有一个名为“1”。@user586011:请在下面添加您的解决方案作为答案并接受它。不要把答案放在这个问题上,那样做不好。很高兴知道你每天都在使用CURL。我试着设置一个socks代理,它在我的本地机器上工作,但在我的linux专用服务器上不工作。有什么想法吗?@coding_?出于安全原因,大多数web主机都会阻止不是80或443的端口。不过我已经解决了这个问题。我相信其他人会从中受益。@GravyCode:如果我们从某些服务获得代理,那么我需要传递用户名/密码吗?我如何知道代理端口是否被网络主机阻止?这些注释很有用,但其他人应该注意,实际上不需要额外的选项。这对我有帮助:curl\u setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//假https@villamejia但是,在使用CURLOPT_SSL_VERIFYPEER=false时要小心。这意味着cURL在连接到https服务器时不会进行任何证书检查,从而使连接容易受到中间人攻击–因此数据安全不再得到保证。最好使用CURLOPT_CAPATH给出一个包含一组有效根证书颁发机构的目录(例如,Debian/Ubuntu上的
/etc/ssl/certs
) root@APPLICATIOSERVER:/var/www/html# php connectiontest.php 61e23468-949e-4103-8e08-9db09249e8s1 OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to 10.172.123.1:80 root@APPLICATIOSERVER:/var/www/html# $proxy = '10.172.123.1:80'; curl_setopt($cSession, CURLOPT_PROXY, $proxy); // PROXY details with port