php利用curl发送 post get del put patch 请求

因为需要在php开发中对接其它接口需要用php  curl去对接其它接口  我把他们封装成函数 希望能对大家有所帮助

这里面是封装好的  会自动把data进行转成json格式   同时解码成php数组输出

 1 function geturl($url){
 2         $headerArray =array("Content-type:application/json;","Accept:application/json");
 3         $ch = curl_init();
 4         curl_setopt($ch, CURLOPT_URL, $url);
 5         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
 6         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
 7         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 8         curl_setopt($url,CURLOPT_HTTPHEADER,$headerArray);
 9         $output = curl_exec($ch);
10         curl_close($ch);
11         $output = json_decode($output,true);
12         return $output;
13 }
14 
15 
16 function posturl($url,$data){
17         $data  = json_encode($data);    
18         $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
19         $curl = curl_init();
20         curl_setopt($curl, CURLOPT_URL, $url);
21         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
22         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
23         curl_setopt($curl, CURLOPT_POST, 1);
24         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
25         curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
26         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
27         $output = curl_exec($curl);
28         curl_close($curl);
29         return json_decode($output,true);
30 }
31 
32 
33 function puturl($url,$data){
34     $data = json_encode($data);
35     $ch = curl_init(); //初始化CURL句柄 
36     curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
37     curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
38     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出 
39     curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); //设置请求方式
40     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
41     $output = curl_exec($ch);
42     curl_close($ch);
43     return json_decode($output,true);
44 }
45 
46 function delurl($url,$data){
47     $data  = json_encode($data);
48     $ch = curl_init();
49     curl_setopt ($ch,CURLOPT_URL,$put_url);
50     curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
51     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
52     curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");   
53     curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
54     $output = curl_exec($ch);
55     curl_close($ch);
56     $output = json_decode($output,true);
57 }


function patchurl($url,$data){
    $data  = json_encode($data);
    $ch = curl_init();
    curl_setopt ($ch,CURLOPT_URL,$url);
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PATCH");   
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);     //20170611修改接口,用/id的方式传递,直接写在url中了
    $output = curl_exec($ch);
    curl_close($ch);
    $output = json_decode($output);
    return $output;
}

 



 

posted @ 2017-03-17 11:00  空为莫  阅读(11017)  评论(0编辑  收藏  举报