php - Guzzle 无法向本地主机(端口 : 80, 8000、8080 等)发出 GET 请求

标签 php laravel http guzzle laravel-artisan

目前使用 Laravel 5.5 和与 laravel 安装程序一起提供的 Guzzle。

我正在尝试发出 GET 请求(其他 HTTP 请求也会出错)但似乎不起作用。

此代码无效:

public function callback(Request $request)
{
    $code = $request->code;

    $client = new Client(['exceptions' => false]);

    try {
      $response = $client->request('GET', 'http://localhost/api/tests');
        // $response = $http->request('POST', Config::get('app.url') . '/oauth/token', [
        //     'form_params' => [
        //         'grant_type' => 'authorization_code',
        //         'client_id' => Config::get('oauth_client.client_id'),
        //         'client_secret' => Config::get('oauth_client.client_secret'),
        //         'redirect_uri' => Config::get('oauth_client.redirect_uri'),
        //         'code' => $code,
        //     ],
        // ]);
        // return json_decode((string) $response->getBody(), true);
    } catch (\Exception $e) {
        dd($e);
    }
    dd($response->getBody());
    return;
}

但是下面这段代码工作得很好

public function callback(Request $request)
{
    $code = $request->code;

    $client = new Client(['exceptions' => false]);

    try {
      $response = $client->request('GET', 'https://www.google.co.id');
        // $response = $http->request('POST', Config::get('app.url') . '/oauth/token', [
        //     'form_params' => [
        //         'grant_type' => 'authorization_code',
        //         'client_id' => Config::get('oauth_client.client_id'),
        //         'client_secret' => Config::get('oauth_client.client_secret'),
        //         'redirect_uri' => Config::get('oauth_client.redirect_uri'),
        //         'code' => $code,
        //     ],
        // ]);
        // return json_decode((string) $response->getBody(), true);
    } catch (\Exception $e) {
        dd($e);
    }
    dd($response->getBody());
    return;
}

我不明白为什么我的 Guzzle 能够请求 google.com 但无法连接到我自己的本地主机服务器(到所有端口)。

任何帮助将不胜感激。

谢谢,

最佳答案

它不起作用的原因是 php artisan serve 使用 PHP 内置 Web 服务器,这是 single threaded .因此,如果您运行您的应用程序,它在完成初始请求之前无法发出另一个请求(您的 Guzzle 请求)。这就是它挂起的原因(如前所述 here )。

一个解决方案是(如您所指出的)使用真正的多线程网络服务器。

但如果您仍想使用 php artisan serve 而不是像 Nginx 这样的网络服务器,有一个简单的解决方案(我已经在 another issue 中发布):

您可以使用另一个端口运行另一个 Web 服务器实例,并将您的应用程序配置为在连接到您的 API 时使用此 base_uri:

php artisan serve \\ defaults to port 8000
\\ in another console
php artisan serve --port=8001
$client->request('GET', 'http://localhost:8001/api/tests')

关于php - Guzzle 无法向本地主机(端口 : 80, 8000、8080 等)发出 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48841018/

相关文章:

php - 从 laravel Eloquent 数据透视表中获取计数

php - 如何手动命名 Laravel 迁移以便 "php artisan migrate"可以运行它?

c - 如何用 curl 做 keepalive http 请求?

apache - .htaccess - 将 http 重定向到 https 排除特定的 url

.net - 在 IIS 中修改 Access-Control-Allow-Origin header

javascript - 我的代码(Javascript)有什么问题?

php - 使用 attr() 在不同的屏幕尺寸上响应式地显示不同的背景图像

php - Laravel 排序关系

php - 从短代码函数内部调用 WordPress get_template_part 首先呈现模板

php - 变量不存在时隐藏内容