[关闭]
@xxliixin1993 2015-12-30T01:54:05.000000Z 字数 4187 阅读 1686

php curl

php


CURL流程图

Created with Raphaël 2.1.2curl_init() 初始化curl_setopt() 设置请求参数curl_exec() 执行并获取结果 curl_close() 释放CURL句柄End

GET简单例子:

  1. <?php
  2. //GET
  3. //初始化
  4. $ch = curl_init();
  5. //设置选项,包括URL,默认是以GET请求
  6. curl_setopt($ch, CURLOPT_URL, "www.baidu.com");
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  8. curl_setopt($ch, CURLOPT_HEADER, 0);
  9. //执行并获取HTML文档内容
  10. $output = curl_exec($ch);
  11. //释放curl句柄
  12. curl_close($ch);
  13. //打印获得的数据
  14. print_r($output);

POST简单例子:

  1. curl.php
  2. <?php
  3. //POST
  4. //初始化
  5. $curl = curl_init();
  6. $url='www.test.com/test.php';
  7. $data=array(
  8. "testDate" => "hello world",
  9. );
  10. //设置抓取的url
  11. curl_setopt($curl, CURLOPT_URL, $url);
  12. //设置头文件的信息作为数据流输出
  13. curl_setopt($curl, CURLOPT_HEADER, 1);
  14. //设置获取的信息以文件流的形式返回,而不是直接输出。
  15. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  16. //设置post方式提交
  17. curl_setopt($curl, CURLOPT_POST, 1);
  18. //设置post数据
  19. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  20. //执行命令
  21. $data = curl_exec($curl);
  22. //关闭URL请求
  23. curl_close($curl);
  24. //显示获得的数据
  25. print_r($data);
  26. ?>
  27. www.test.php/test.php
  28. <?php
  29. echo $_POST['testDate'];
  30. ?>

输出:

HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Mon, 23 Nov 2015 04:50:26 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.6.15

hello world

curl_setopt()函数:

curl_setopt ( resource $ch , int $option , mixed $value )
选项 说明
CURLOPT_HEADER 启用时会将头文件的信息作为数据流输出
CURLOPT_AUTOREFERER 当根据Location:重定向时,自动设置header中的Referer:信息。(可选true或false)
CURLOPT_POST 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样
CURLOPT_POSTFIELDS 全部数据使用HTTP协议中的"POST"操作来发送。要发送文件,在文件名前面加上@前缀并使用完整路径。这个参数可以通过urlencoded后的字符串类似'para1=val1&para2=val2&...'或使用一个以字段名为键值,字段数据为值的数组。如果value是一个数组,Content-Type头将会被设置成multipart/form-data
CURLOPT_PUT 启用时允许HTTP发送文件,必须同时设置CURLOPT_INFILE和CURLOPT_INFILESIZE
CURLOPT_INFILE 在上传文件的时候需要读取的文件地址,值是一个资源类型
CURLOPT_INFILESIZE 设定上传文件的大小限制,字节(byte)为单位
CURLOPT_BINARYTRANSFER true表示用返回原始输出
CURLOPT_FAILONERROR 显示HTTP状态码,默认行为是忽略编号小于等于400的HTTP信息
CURLOPT_FORBID_REUSE 在完成交互以后强迫断开连接,不能重用
CURLOPT_FRESH_CONNECT 强制获取一个新的连接,替代缓存中的连接
CURLOPT_NOBODY 启用时将不对HTML中的BODY部分进行输出
CURLOPT_FILETIME 启用时会尝试修改远程文档中的信息。结果信息会通过curl_getinfo()函数的CURLINFO_FILETIME选项返回。 curl_getinfo()
CURLOPT_CRLF 启用时将Unix的换行符转换成回车换行符
CURLOPT_COOKIESESSION 启用时curl会仅仅传递一个session cookie,忽略其他的cookie,默认状况下cURL会将所有的cookie返回给服务端。session cookie是指那些用来判断服务器端的session是否有效而存在的cookie

curl_getinfo()函数:
获取一个cURL连接资源句柄的信息

curl_getinfo ( resource $ch [, int $opt = 0 ] )

例:
获取响应码:

$responseData = curl_getinfo($ch); 
Array

(
[url] => HTTP://m.fang.com/
[content_type] => text/html; charset=gb2312
[http_code] => 200
[header_size] => 265
[request_size] => 49
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.143788
[namelookup_time] => 0.012269
[connect_time] => 0.018964
[pretransfer_time] => 0.019034
[size_upload] => 0
[size_download] => 69795
[speed_download] => 485402
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => 0
[starttransfer_time] => 0.029366
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 124.251.50.91
[certinfo] => Array
(
)

[primary_port] => 80
[local_ip] => 192.168.110.164
[local_port] => 40025

)

详细参数说明见php官方手册

封装的Curl类

  1. <?php
  2. class Curl {
  3. /**
  4. * curl句柄
  5. */
  6. public $curl;
  7. /**
  8. * 响应数据
  9. */
  10. public $output;
  11. /**
  12. * 请求url
  13. */
  14. public $url;
  15. public function __construct($url){
  16. //初始化
  17. $this->curl = curl_init();
  18. $this->url = $url;
  19. }
  20. public function getMethod($cookie=''){
  21. //设置选项,包括URL
  22. curl_setopt($this->curl, CURLOPT_HEADER, 1); // 返回header部分
  23. //重定向
  24. curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
  25. curl_setopt($this->curl, CURLOPT_URL, $this->url);
  26. //User-agent设置
  27. curl_setopt($this->curl, CURLOPT_USERAGENT, 'LearnForPHP');
  28. //超时时长
  29. curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
  30. //设置自定义头部
  31. curl_setopt($this->curl, CURLOPT_HTTPHEADER, array( "Content-type: text/xml"));
  32. if (!empty($cookie)){
  33. curl_setopt($this->curl, CURLOPT_COOKIE,$cookie);
  34. }
  35. //curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);        
  36. }
  37. /**
  38. * $data array
  39. */
  40. public function postMethod($data){
  41. //设置抓取的url
  42. curl_setopt($this->curl, CURLOPT_URL, $this->url);
  43. //设置头文件的信息作为数据流输出
  44. curl_setopt($this->curl, CURLOPT_HEADER, 1);
  45. //设置获取的信息以文件流的形式返回,而不是直接输出。
  46. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
  47. //设置post方式提交
  48. curl_setopt($this->curl, CURLOPT_POST, 1);
  49. //设置post数据
  50. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
  51. //curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1); //重定向
  52. }
  53. public function exec(){
  54. //执行并获取HTML文档内容
  55. $this->output = curl_exec($this->curl);
  56. //释放curl句柄
  57. curl_close($this->curl);
  58. return $this->output;
  59. }
  60. }
  61. $curl = new Curl('https://www.zhihu.com/');
  62. $curl->getMethod($cookie);
  63. print_r($curl->exec());
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注