Editor’s Choice: GuzzleHttp

{info} 替代CURL 次于app()->handle

获取返回数据

// form_params填入参数
$response = $client->request('POST', 'getUsersByLevel', [
    'headers' => $headers,
    'form_params' => [
        'level' => $userInfo['department_info'][0]['department_level']
    ]
]);
# 获取返回数据需参考文档/源码 并非直接打印response
$result = $response->getBody()->getContents();

截获异常并返回记录异常信息

# 异常详见http://docs.guzzlephp.org/en/stable/quickstart.html#exceptions
catch ( \GuzzleHttp\Exception\RequestException $e ) {
    // 发送网络错误(连接超时、DNS错误等)时
    echo $e->getRequest();
    if ($e->hasResponse()) {
        echo $e->getResponse();
    }
}
catch (\GuzzleHttp\Exception\ConnectException $e) {
    // 返回连接错误/网络错误比如url不对
    // ConnectException继承自RequestException
    Log::error( $e->getMessage() );
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
    // 返回400 500 错误
    // ClientException for 400-level errors
    // ServerException for 500-level errors
    // BadResponseException = ClientException + ServerException
    Log::error( $e->getMessage() );
}