POST

<?php
/**
 * curl POST请求
 * @param string $url 请求地址
 * @param array $data 请求数据
 * @param bool $json 请求格式[urlencoded|json]
 * @return bool|string
 */
function curlPost(string $url, array $data, bool $json = true)
{

    if ($json) {
        $str_data = json_encode($data, JSON_UNESCAPED_UNICODE);
        $header = ['Content-Type: application/json'];
    } else {
        // 默认选项
        $str_data = http_build_query($data);
        $header = ['Content-Type: application/x-www-form-urlencoded'];
    }
    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $str_data,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
    ]);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

GET

<?php
/**
 * curl GET请求
 */
function curlGet($url){
    $ch = curl_init($url);
    curl_setopt_array($ch,[
        CURLOPT_RETURNTRANSFER => true,
    ]);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

标签: none

添加新评论