免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2725 | 回复: 2
打印 上一主题 下一主题

php curl常用函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-09 23:23 |只看该作者 |倒序浏览
php curl常用函数



php curl常用于:GET,POST,HTTP验证,302重定向,设置cURL的代理。

1、开启PHP的cURL功能
在Windows平台下,或者使用xampp之类的集成服务器的程序,会非常简单,你需要改一改你的php.ini文件的设置,找到php_curl.dll,并取消前面的分号注释就行了。如下所示:

//取消注释,开启cURL功能
extension=php_curl.dll

在Linux下面,那么,你需要重新编译你的PHP了,编辑时,你需要打开编译参数——在configure命令上加上“–with-curl” 参数。

2、使用cURL来GET数据
cURL最简单最常用的采用GET来获取网页内容的PHP函数
  1. function getCURL($url){
  2. $curl = curl_init();
  3. curl_setopt($curl, CURLOPT_URL, $url);
  4. curl_setopt($curl, CURLOPT_HEADER, 0);
  5. curl_setopt($curl, CURLOPT_TIMEOUT, 3);//超时时间
  6. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  7. $data = curl_exec($curl);
  8. curl_close($curl);
  9. return $data;
  10. }
复制代码
3、使用cURL来POST数据
当我们需要对cURL请求的页面采用POST的请求方式时,我们使用下面的PHP函数
  1. function _curl_post($url, $vars) {
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  4. curl_setopt($ch, CURLOPT_URL, $url);
  5. curl_setopt($ch, CURLOPT_POST, 1);
  6. curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
  7. $data = curl_exec($ch);
  8. curl_close($ch);
  9. if ($data)
  10. return $data;
  11. else
  12. return false;
  13. }
复制代码
4、使用cURL,需要HTTP服务器认证
当我们请求地址需要加上身份验证,即HTTP服务器认证的时候,我们就要使用下面的函数了,对于cURL中GET方法使用验证也是采用相同的方式。
  1. function postCurlHTTP($url, $str) {
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_URL, $url);
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  5. curl_setopt($ch, CURLOPT_POST, 1);
  6. curl_setopt($ch, CURLOPT_USERPWD, “验证的用户名:密码”);
  7. curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
  8. $data = curl_exec($ch);
  9. $Headers = curl_getinfo($ch);
  10. if ($Headers['http_code'] == 200) {
  11. return $data;
  12. } else {
  13. return false;
  14. }
  15. }
复制代码
5、使用cURL获取302重定向的页面
下面函数$data为重定向后页面的内容,这里我们写一个简单的cURL POST的302重定向后返回重定向页面URL的函数,有时候返回页面的URL更加重要。
  1. function _curl_post_302($url, $vars) {
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  4. curl_setopt($ch, CURLOPT_URL, $url);
  5. curl_setopt($ch, CURLOPT_POST, 1);
  6. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
  7. curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
  8. $data = curl_exec($ch);
  9. $Headers = curl_getinfo($ch);
  10. curl_close($ch);
  11. if ($data&&$Headers)
  12. return s$Headers["url"];
  13. else
  14. return false;
  15. }
复制代码
6、给cURL加个代理服务器
  1. $ch = curl_init();
  2. curl_setopt($ch, CURLOPT_URL, ‘http://www.js8.in‘);
  3. curl_setopt($ch, CURLOPT_HEADER, 1);
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  5. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  6. curl_setopt($ch, CURLOPT_PROXY, ‘代理服务器地址(www.js8.in):端口’);
  7. curl_setopt($ch, CURLOPT_PROXYUSERPWD, ‘代理用户:密码’);
  8. $data = curl_exec();
  9. curl_close($ch);
复制代码
7、一个cURL简单的类
  1. <?php
  2. /*
  3. Sean Huber CURL library

  4. This library is a basic implementation of CURL capabilities.
  5. It works in most modern versions of IE and FF.

  6. ==================================== USAGE ====================================
  7. It exports the CURL object globally, so set a callback with setCallback($func).
  8. (Use setCallback(array(’class_name’, ‘func_name’)) to set a callback as a func
  9. that lies within a different class)
  10. Then use one of the CURL request methods:

  11. get($url);
  12. post($url, $vars); vars is a urlencoded string in query string format.

  13. Your callback function will then be called with 1 argument, the response text.
  14. If a callback is not defined, your request will return the response text.
  15. */

  16. class CURL {
  17. var $callback = false;

  18. function setCallback($func_name) {
  19. $this->callback = $func_name;
  20. }

  21. function doRequest($method, $url, $vars) {
  22. $ch = curl_init();
  23. curl_setopt($ch, CURLOPT_URL, $url);
  24. curl_setopt($ch, CURLOPT_HEADER, 1);
  25. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  26. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  27. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  28. curl_setopt($ch, CURLOPT_COOKIEJAR, ‘cookie.txt’);
  29. curl_setopt($ch, CURLOPT_COOKIEFILE, ‘cookie.txt’);
  30. if ($method == ‘POST’) {
  31. curl_setopt($ch, CURLOPT_POST, 1);
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
  33. }
  34. $data = curl_exec($ch);
  35. curl_close($ch);
  36. if ($data) {
  37. if ($this->callback)
  38. {
  39. $callback = $this->callback;
  40. $this->callback = false;
  41. return call_user_func($callback, $data);
  42. } else {
  43. return $data;
  44. }
  45. } else {
  46. return curl_error($ch);
  47. }
  48. }

  49. function get($url) {
  50. return $this->doRequest(‘GET’, $url, ‘NULL’);
  51. }

  52. function post($url, $vars) {
  53. return $this->doRequest(‘POST’, $url, $vars);
  54. }
  55. }
  56. ?>
复制代码
转自:http://blog.phpmake.com/?p=326

论坛徽章:
0
2 [报告]
发表于 2011-08-10 08:56 |只看该作者
学习了

论坛徽章:
54
2017金鸡报晓
日期:2017-02-08 10:39:42操作系统版块每日发帖之星
日期:2016-03-08 06:20:00操作系统版块每日发帖之星
日期:2016-03-07 06:20:00操作系统版块每日发帖之星
日期:2016-02-22 06:20:00操作系统版块每日发帖之星
日期:2016-01-29 06:20:00操作系统版块每日发帖之星
日期:2016-01-27 06:20:00操作系统版块每日发帖之星
日期:2016-01-20 06:20:00操作系统版块每日发帖之星
日期:2016-01-06 06:20:0015-16赛季CBA联赛之江苏
日期:2015-12-21 20:00:24操作系统版块每日发帖之星
日期:2015-12-21 06:20:00IT运维版块每日发帖之星
日期:2015-11-17 06:20:002015亚冠之广州恒大
日期:2015-11-12 10:58:02
3 [报告]
发表于 2011-08-10 10:50 |只看该作者
正想学习curl的使用,结果一搜索又回到了CU,CU的SEO做非常棒!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP