Guzzle 返回 500 错误

Gra*_*Dev 4 php laravel guzzle laravel-passport

所以我对大量使用和构建 API 很陌生,我使用过 Laravel Passport 并在一个 GET 上调用它很好。我写了一个 POST 调用并收到 500 错误返回

后期功能

  public function newsSingle() {
            $request = (new GuzzleHttp\Client)->post('http://138.68.180.100/news/article/single', [
                'headers' => [
                    'Authorization' => 'Bearer '.session()->get('token.access_token'),
                    'post_id' => $_POST['post_id']
                ]
            ]);
            $news = json_decode((string)$request->getBody());
            return view('pages.newsingle', compact('news'));
}
Run Code Online (Sandbox Code Playgroud)

这确实添加了帖子项目 POST 数据 post_id "3"

另一端我有

路线:

Route::post('news/article/single', 'ApiController@singlePost')->middleware('auth:api');
Run Code Online (Sandbox Code Playgroud)

控制器功能:

public function singlePost(Request $request) {
        $article = Articles::where('id', $request['post_id'])->get();
        return $article;
    } 
Run Code Online (Sandbox Code Playgroud)

我的错误:

Server error: `POST http://ipaddress/news/article/single` resulted in a `500 Internal Server Error` response: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta name="robots" content="noindex,nofollow (truncated...)
Run Code Online (Sandbox Code Playgroud)

小智 7

500当获取响应代码并抛出异常时,我们发现 Guzzle 的外部 API 调用也存在类似问题Server error:。有一种解决方法可以通过捕获由于BadResponseException返回作为响应而导致的异常来执行绕过机制。下面是执行此操作的代码。:)

catch (\GuzzleHttp\Exception\BadResponseException $e) {
    return $e->getResponse()->getBody()->getContents();
}
Run Code Online (Sandbox Code Playgroud)