当前位置: 首页 > 工具软件 > PHP Forward > 使用案例 >

PHP 发送请求(curl)

向子安
2023-12-01

自己封装

<?php

namespace common\publics;

class Request
{
    /**
     * Get请求方法(http)
     * @param $url
     * @return bool|string
     */
    public static function getRequest($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 10);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }

    /**
     * 请求方式
     * @param $url
     * @param $type
     * @param array $data
     * @return bool|string
     */
    public static function httpCurl($url,$data=[],$type="POST"){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        if($type == 'GET'){
            curl_setopt($curl, CURLOPT_HEADER, 1);        //设置头文件的信息作为数据流输出
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回,而不是直接输出
        }
        if($type == 'POST'){
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    /**
     * curl http/https 请求
     * @param string $url
     * @param string|array $data
     * @param bool $isPost
     * @param int $execTimes
     * @param bool $makeJsonStr
     * @return mixed|string
     */
    public static function curlHttp($url, $data = '', $isPost = true, $execTimes = 1, $makeJsonStr = true)
    {
        // 检测是不是 https
        $ssl = false;
        $http = parse_url($url,PHP_URL_SCHEME);
        if('https' == $http){
            $ssl = true;
        }
        // 检测 url 中是否已存在参数
        $mark = strpos($url,'?');
        // 处理 POST 请求的参数
        $header = array();
        if($isPost){
            if(is_array($data) && $makeJsonStr){
                $data = json_encode($data,JSON_UNESCAPED_UNICODE);
            }
            if($makeJsonStr){
                $header[] = 'Content-Type: application/json';
            }
        }
        // 处理 GET 请求的参数
        else{
            // 将参数转为请求字符串
            if(is_array($data)){
                $data = http_build_query($data);
            }
            $conn = '&';
            if(false === $mark){
                $conn = '?';
            }
            $url .= $conn . $data;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        if($ssl){
            curl_setopt($ch, CURLOPT_SSLVERSION, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        if($isPost) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        if( ! empty($header)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        # 特殊接口可能会多次请求才能得到正确返回结果,例:微信APP支付
        for($i = 0 ; $i < $execTimes ; $i++){
            $res = curl_exec($ch);
        }
        if (curl_errno($ch)) {
            $res = curl_error($ch);
        }
        curl_close($ch);
        return $res;
    }


    /**
     * 发送Json对象数据
     * @param $url
     * @param array $data
     * @param $header
     * @return bool|string
     */
    public static function httpCurlPostHeader($url, $data, $header)
    {
        if (is_array($data)) {
            $data = json_encode($data);
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);//超时时间
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    /**
     * 带body和header参数的请求(防沉迷请求)
     * @param $url
     * @param array $body
     * @param array $header
     * @param string $method
     * @return bool|string
     */
    public static function requestCurlPostHeader($url,$body = [],$header=['Content-Type: application/json;charset=utf-8'],$method="post"){

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        // 从证书中检查SSL加密算法是否存在(默认不需要验证)
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if($method == "post"){
            curl_setopt($ch, CURLOPT_POST, 1);
            if($body){
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
            }
        }
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }



}

菜鸟教程

<?php
function geturl($url){
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
}


function posturl($url,$data){
        $data  = json_encode($data);    
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output,true);
}


function puturl($url,$data){
    $data = json_encode($data);
    $ch = curl_init(); //初始化CURL句柄 
    curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); //设置请求方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output,true);
}

function delurl($url,$data){
    $data  = json_encode($data);
    $ch = curl_init();
    curl_setopt ($ch,CURLOPT_URL,$put_url);
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");   
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    $output = curl_exec($ch);
    curl_close($ch);
    $output = json_decode($output,true);
}

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;
}
?>

 

 类似资料: