【问题标题】:Guzzle returns 500 errorGuzzle 返回 500 错误
【发布时间】:2018-06-01 18:28:09
【问题描述】:

所以我是新来的 guzzle 和构建 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'));
}

添加帖子项 发布数据 post_id “3”

在另一端我有

路线:

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

控制器功能:

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

我的错误:

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...)

【问题讨论】:

  • 那么您必须检查站点日志中的错误消息。 “内部服务器错误”的意思是“程序员搞砸了一些我不会告诉你的事情,但会将其存储在日志文件中,以便他知道细节以及如何修复它”。
  • 确保在你的 .env 中调试设置为 true,这样你就可以看到你的错误。
  • 所以我在两个应用程序上都打开了 Debuggin,仍然没有看到 laravel 错误,并且试图在 Nginx 中查找错误日志很痛苦
  • 这可能是一大堆事情,从代码中的简单错字到数据库结构中的不匹配。如果这些是单个应用程序,请确保在 API 端以及您自己的端进行调试,错误将记录在发生 500 错误的位置,而不是您尝试使用 API 的位置。
  • 所以我有 API 端和另一个都在调试,但我什么也没得到,路由和控制器在 api 端工作,我只是在调用时遇到这个错误,这可能与CORS??

标签: php laravel guzzle laravel-passport


【解决方案1】:

当响应代码为 500 并得到 Server error: 并引发异常时,我们发现 Guzzle for External API 调用存在类似问题。有一种解决方法是通过捕获由于BadResponseException 引起的异常以作为响应返回来绕过机制。下面是执行此操作的代码。 :)

catch (\GuzzleHttp\Exception\BadResponseException $e) {
    return $e->getResponse()->getBody()->getContents();
}

【讨论】:

  • 这对我有用。
猜你喜欢
  • 2018-11-26
  • 2015-06-25
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 2018-10-02
  • 2017-11-27
  • 2016-06-06
相关资源
最近更新 更多