PHP 版的 Curl 使用代理服务器示例

By Symphony - Last updated: Wednesday, November 11, 2009 - Save & Share - One Comment

Curl 可以模拟浏览器访问url地址,但是有些网站我们无法直接访问,所以需要使用代理。上PHP 手册查一下对使用 sockets5 只是笼统的介绍了一下,这里贴出 PHP 版的 Curl 使用 sockets5 代理的示例代码,以便日后查阅。

  1. < ?php
  2. $remote_url = 'http://www.google.com';
  3. $ch = curl_init($remote_url);      
  4. curl_setopt($ch, CURLOPT_HEADER, 0);
  5.  
  6. //以下代码设置代理服务器
  7. //是否启用代理
  8. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
  9. //代理认证模式
  10. curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
  11. //代理服务器地址
  12. curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1");
  13. //代理服务器端口
  14. curl_setopt($ch, CURLOPT_PROXYPORT, 8866);
  15. //代理模式,这里用socket5的方式
  16. curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  17.  
  18.  
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  20. $res = curl_exec($ch); // Run it!
  21. curl_close($ch); //关闭curl通道
  22. echo $res;
Posted in Uncategorized • Tags: , Top Of Page

One Response to “PHP 版的 Curl 使用代理服务器示例”

Pingback from GoTop’s Blog » cURL使用Sock5代理
Time 2010/11/19 at 4:05 am

[...] PHP 版的 Curl 使用代理服务器示例 Leave a comment | Trackback No comments yet. [...]

Write a comment