zoukankan      html  css  js  c++  java
  • 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);
    
    ╰︶﹉⋛⋋⊱⋋๑๑⋌⊰⋌⋚﹉︶╯
  • 相关阅读:
    算法导论(第三版)Exercises2.1(插入排序、线性查找、N位大数相加)
    含铝馒头可能损伤儿童的智力
    每秒3600乘以100等于36万次售票解决方案
    namespace Measure
    public interface ICloneable
    VB.net 与线程
    C#调用VP 包含素材
    C# 定时器 一个简单 并且可以直接运行的Demo
    松下 激光位移传感器 API
    在Win7系统下, 使用VS2015 打开带有日文注释程序出现乱码的解决方案
  • 原文地址:https://www.cnblogs.com/zhangcheng001/p/15179171.html
Copyright © 2011-2022 走看看