PHP请求第三方接口的函数

 1 <?php
 2     
 3     public function HttpGet($url){
 4         $curl = curl_init ();
 5         curl_setopt ( $curl, CURLOPT_URL, $url );
 6         curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
 7         // curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );
 8         // curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36');
 9         
10         //如果用的协议是https则打开鞋面这个注释
11         //curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
12         // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
13         
14         $res = curl_exec ( $curl );
15         curl_close ( $curl );
16         return $res;
17     }
18 
19      public function HttpPost($url,$param){
20 
21         $ch = curl_init();
22         //如果$param是数组的话直接用
23         curl_setopt($ch, CURLOPT_URL, $url);
24         //如果$param是json格式的数据,则打开下面这个注释
25         // curl_setopt($ch, CURLOPT_HTTPHEADER, array(
26         //         'Content-Type: application/json',
27         //         'Content-Length: ' . strlen($param))
28         // );
29         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
30         curl_setopt($ch, CURLOPT_POST, 1);
31         curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
32         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
33 
34         //如果用的协议是https则打开鞋面这个注释
35         // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
36         // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
37 
38         $data = curl_exec($ch);
39 
40         curl_close($ch);
41         return $data;
42 
43     }
44     
45 ?>

 

posted @ 2016-11-08 15:38  JeromeZ  阅读(5905)  评论(0编辑  收藏  举报