php curl 提交 总结

1、post https提交 方法 

 1 function curl_post2($url='', $postdata='', $options=array()){
 2     $ch = curl_init($url);
 3      
 4     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 5     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
 6     curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
 7      
 8      
 9     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
10     curl_setopt($ch, CURLOPT_POST, 1);
11     curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
12     curl_setopt($ch, CURLOPT_TIMEOUT, 20);
13     if (!empty($options)){
14         curl_setopt_array($ch, $options);
15     }
16     $data = curl_exec($ch);
17     curl_close($ch);
18     return $data;
19 }

2、普通post提交 

 1 //通过curl模拟post的请求;
 2 function SendDataByCurl($url,$data=array()){
 3     //对空格进行转义
 4     $url = str_replace(' ','+',$url);
 5     $ch = curl_init();
 6     //设置选项,包括URL
 7     curl_setopt($ch, CURLOPT_URL, "$url");
 8     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 9     curl_setopt($ch, CURLOPT_HEADER, 0);
10     curl_setopt($ch,CURLOPT_TIMEOUT,3);  //定义超时3秒钟 
11      // POST数据
12     curl_setopt($ch, CURLOPT_POST, 1);
13     // 把post的变量加上
14     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));    //所需传的数组用http_bulid_query()函数处理一下,就ok了
15      
16     //执行并获取url地址的内容
17     $output = curl_exec($ch);
18     $errorCode = curl_errno($ch);
19     //释放curl句柄
20     curl_close($ch);
21     if(0 !== $errorCode) {
22         return false;
23     }
24     return $output;
25  
26 }
27  
28 function curl_post($url='', $postdata='', $options=array()){
29     $ch = curl_init($url);
30     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
31     curl_setopt($ch, CURLOPT_POST, 1);
32     curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
33     curl_setopt($ch, CURLOPT_TIMEOUT, 5);
34     if (!empty($options)){
35         curl_setopt_array($ch, $options);
36     }
37     $data = curl_exec($ch);
38     curl_close($ch);
39     return $data;
40 }

3、curl get提交

 1 function curl_get($url='', $options=array()){
 2     $ch = curl_init($url);
 3     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 4     curl_setopt($ch, CURLOPT_TIMEOUT, 5);
 5     if (!empty($options)){
 6         curl_setopt_array($ch, $options);
 7     }
 8     $data = curl_exec($ch);
 9     curl_close($ch);
10     return $data;
11 }

参考:

 1 function vpost($url,$data,$cookie){ // 模拟提交数据函数
 2     $curl = curl_init(); // 启动一个CURL会话
 3     curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
 4     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
 5     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
 6     curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
 7     curl_setopt($curl, CURLOPT_COOKIE, $cookie);
 8     curl_setopt($curl, CURLOPT_REFERER,'https://www.baidu.com');// 设置Referer
 9     curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
10     curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
11     curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
12     curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
13     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
14     $tmpInfo = curl_exec($curl); // 执行操作
15     if (curl_errno($curl)) {
16        echo 'Errno'.curl_error($curl);//捕抓异常
17     }
18     curl_close($curl); // 关闭CURL会话
19     return $tmpInfo; // 返回数据
20 }

 

posted @ 2016-11-30 09:53  今天、天气晴  阅读(196)  评论(0编辑  收藏  举报