php的POST GET PUT请求
POST
function curl_post($url, $header, $content) { $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content)); $response = curl_exec($ch); if ($error = curl_error($ch)) { die($error); } curl_close($ch); return $response; }
GET
//get请求 public static function curl_get($url, $header) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //参数为1表示传输数据,为0表示直接输出显示。 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //参数为0表示不带头文件,为1表示带头文件 // curl_setopt($ch, CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $output = curl_exec($ch); curl_close($ch); return $output; }
PUT
public static function curl_put($data) { $AppId = '21081121580006C4'; $AppSecret = '81d1eccc9498f17386ea5724a9d7935fa7d436b8'; $AppTimestamp = time(); $url = 'http://xxx.cn/api/supplier/order/v2/order'; $api = '/api/supplier/order/v2/order'; $AppToken = sha1($AppId . $AppSecret . $api . $AppTimestamp); //头部 $header = array('AppId:' . $AppId, 'AppToken:' . $AppToken, 'AppTimestamp:' . $AppTimestamp); // $data = [ // 'orderSN' => '21081409560004A4', // 'state' => 2, // 'remarks' => '' // ]; $data = json_encode($data); $ch = curl_init(); // $header[] = "Content-type:image/jpeg"; //定义header,可以加多个 curl_setopt($ch, CURLOPT_URL, $url); //定义请求地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); //定义请求类型,当然那个提交类型那一句就不需要了 curl_setopt($ch, CURLOPT_HEADER, 0); //定义是否显示状态头 1:显示 ; 0:不显示 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //定义header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //定义是否直接输出返回流 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定义提交的数据 $res = curl_exec($ch); curl_close($ch); //关闭 return $res; }
如果是https请求 加入这一行 不校验SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
╰︶﹉⋛⋋⊱⋋๑๑⋌⊰⋌⋚﹉︶╯