Guzzle无法向本地主机发出GET请求(端口:80、8000、8080等) [英] Guzzle Cannot make GET request to the localhost (port: 80, 8000, 8080, etc )

查看:566
本文介绍了Guzzle无法向本地主机发出GET请求(端口:80、8000、8080等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前使用的是Laravel安装程序随附的Laravel 5.5和Guzzle.

Currently using Laravel 5.5 and Guzzle that comes together with the laravel installer.

我正在尝试发出GET请求(其他HTTP请求也会发生错误),但似乎无法正常工作.

I am trying to make GET request (error occur with other HTTP requests as well) but don't seem work.

此代码不起作用:

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;
}

但是下面的这段代码很好用

But this code below is work very well

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却无法连接到我自己的本地主机服务器(连接到所有端口).

I'm not understand why my Guzzle able to request to google.com but unable to connect to my own localhost server (to all ports).

任何帮助将不胜感激.

谢谢

推荐答案

将Apache2和虚拟主机用于我的开发服务器可以解决此问题,这种方法比对我的Guzzle参数进行任何更改都更现实.

Using Apache2 and Virtual Host for my development server solved the problem, this approach more realistic than making any changes on my Guzzle parameters.

因此,我的结论是,最好使用真正的Web服务器(Nginx,Apache),而不要使用工匠服务.

So my conclusion is Better to use real web server (Nginx, Apache) rather than artisan serve.

这篇关于Guzzle无法向本地主机发出GET请求(端口:80、8000、8080等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆