php利用curl發送 post get del put patch 請求

由於須要在php開發中對接其它接口須要用php  curl去對接其它接口  我把他們封裝成函數 但願能對你們有所幫助php

這裏面是封裝好的  會自動把data進行轉成json格式   同時解碼成php數組輸出json

 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;
}
相關文章
相關標籤/搜索