首页 > Web开发 > 详细

php发起http请求

时间:2020-05-19 16:08:33      阅读:47      评论:0      收藏:0      [点我收藏+]
    public function request($url,$method = "get",$data = []){
        $content = $data;
        $header = "Content-type:application/json";
        if(is_array($data)){
            $content = http_build_query($data);
            $header = "Content-type:application/x-www-form-urlencoded";
        }

        $opts = [
            "http" => [  //这个位置不能填https
                ‘method‘ => $method,  //请求方法
                ‘header‘ => $header,  //header头信息
                "content" => $content  //数据
            ]
        ];

        $context = stream_context_create($opts); //构建资源流上下文

        $result = file_get_contents($url,false,$context); //把文件读入一个字符串

        return $result;
    }

 

php发起http请求

原文:https://www.cnblogs.com/jint-php7/p/12917474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!