php 适合get、post、put、delete、head请求(复制可用)

IT-Pony 2017-03-15 PM 531℃ 0条

<?php

    function http($url,$data = '',$method = 'GET',$headers = array())
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //判断一下是不是https
        if(strpos($url,'https') == 0){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        //自定义请求方式
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //定义请求类型,当然那个提交类型那一句就不需要了
        //如果数据不为空就发送数据
        !empty($data) || curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        //发送自定义header头
        !empty($headers) || curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//定义header
    
        $results = curl_exec($ch);
        curl_close($ch);
    
        return $results;
    }
标签: none

非特殊说明,本博所有文章均为博主原创。

评论啦~