如何通过代理使用CURL?

use*_*011 121 php proxy curl php-curl

我想设置curl使用代理服务器.网址由html表单提供,这不是问题.没有代理,它工作正常.我在这个和其他网站上找到了代码,但它们不起作用.任何帮助找到正确的解决方案将非常感激.我觉得波纹管很接近,但我错过了一些东西.谢谢.

我在这里改编的波纹管代码http://www.webmasterworld.com/forum88/10572.htm,但它返回了第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);
?>
Run Code Online (Sandbox Code Playgroud)

波纹管从卷曲到代理返回没有内容

<?

$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;
?>
Run Code Online (Sandbox Code Playgroud)

目前在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;
?> 
Run Code Online (Sandbox Code Playgroud)

小智 216

这是一个删除了错误的工作版本.

$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;
Run Code Online (Sandbox Code Playgroud)

我添加CURLOPT_PROXYUSERPWD了以防任何代理需要用户名和密码.我设置CURLOPT_RETURNTRANSFER为1,以便将数据返回到$curl_scraped_page变量.

我删除了第二个额外的curl_exec($ch);,它将停止返回的变量.我将您的代理IP和端口整合到一个设置中.

我也删除了CURLOPT_HTTPPROXYTUNNEL,CURLOPT_CUSTOMREQUEST因为它是默认的.

如果您不想返回标题,请注释掉CURLOPT_HEADER.

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

curl_setopt($ch, CURLOPT_PROXY, null);
Run Code Online (Sandbox Code Playgroud)

任何问题都可以随意提问,我cURL每天都在工作.


Som*_*luk 32

我已经解释了使用CURL PROXY所需的各种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;
Run Code Online (Sandbox Code Playgroud)

  • 这些评论很有帮助,但其他人应该注意到其他选项实际上并非*必需*. (3认同)
  • `CURLPROXY_SOCKS5`解决了我的问题.谢谢 (3认同)