码迷,mamicode.com
首页 > Web开发 > 详细

PHP CURL POST、GET

时间:2018-06-11 22:03:19      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:pos   field   php curl   param   header   ssl   func   inf   []   

 1 <?php
 2     /**
 3      * CURL HTTP.POST访问
 4      * @param $url
 5      * @param array $param
 6      * @return mixed
 7      */
 8 public function curl_post_http($url, $param = [])
 9 {
10     $ch = curl_init();//初始化curl
11     curl_setopt($ch, CURLOPT_URL, $url); 
12     curl_setopt($ch, CURLOPT_HEADER, 0);
13     curl_setopt($ch, CURLOPT_TIMEOUT, 30);// 设置超时
14     curl_setopt($ch, CURLOPT_POST, 1);// POST方式提交
15     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
16     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
17     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 执行成功将结果返回,不自动输出任何内容
18     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));// POST 提交参数
19     $res = curl_exec($ch);//执行并获取HTML文档内容
20     curl_close($ch); //释放curl句柄
21     return $res;// 返回HTML文档
22 }
23 
24 /**
25  * CURL HTTP.GET访问
26  * @param $url
27  * @param array $setopt
28  * @return mixed
29  */
30 public function curl_get_http($url, $setopt = [])
31 {
32     $curl = curl_init(); // 初始化curl
33     curl_setopt($curl, CURLOPT_URL, $url);
34     curl_setopt($curl, CURLOPT_HEADER, 0);
35     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
36     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
37     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
38     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
39     curl_setopt($curl, CURLOPT_TIMEOUT, 30); //设置超
40     curl_setopt_array($curl, $setopt);//设置协议
41     $tmpInfo = curl_exec($curl); 
42     //释放curl句柄
43     curl_close($curl);
44     return $tmpInfo;
45 }

 

PHP CURL POST、GET

标签:pos   field   php curl   param   header   ssl   func   inf   []   

原文地址:https://www.cnblogs.com/fbio/p/9169062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!