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

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

你可能感兴趣的:(php(二):请求接口并接受返回数据)