php(二):请求接口并接受返回数据

北城以北 提交于 2020-03-01 03:09:27

php请求其它接口并接受返回值实例:

$auth = '';         //请求接口
$post_info = [];   //请求数据数组
$post_info['refresh_token'] = $refresh_token;
$post_info = json_encode($post_info, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$result0 = curl_post($auth, $post_info);
$result0 = json_decode($result0, true);

function curl_post($url, $body)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $body,
        CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json",
            "Postman-Token: cb61dcfa-e807-4e1e-a88d-a0756d584aac",
            "cache-control: no-cache"
        ),
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    if ($err) {
        die("curl err: " . $err);
    } else {
        return $response;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!