laravel API

/**
 * [api]
 * @author Foreach
 * @param  string  $method   [请求方式]
 * @param  string  $url      [地址]
 * @param  array   $params   [参数]
 * @return
 */
function api($method = 'POST', $url = '', $params = array())
{
    $client = new Client([
        'base_uri' => config('app.url')
    ]);

    $headers = ['Accept' => 'application/json', 'Authorization' => 'Bearer '. Session::get('token')];
    $data['headers'] = $headers;
    if ($method == 'GET') {
        $data['query'] = $params;
    } else {
        $data['form_params'] = $params;
    }
    $data['http_errors'] = false;

    // try {
        $response = $client->request($method, $url, $data);
    // } catch (TransferException $e) {
    //     abort(500);
    // }

    return json_decode($response->getBody(), true);
}

调用(pc端):

 

posted @ 2019-05-21 17:34  心之所依  阅读(204)  评论(0编辑  收藏  举报