【问题标题】:How to send cURL request to Laravel App via Guzzle如何通过 Guzzle 向 Laravel 应用程序发送 cURL 请求
【发布时间】:2020-01-18 10:55:40
【问题描述】:

我在我的 Laravel 应用程序中使用 Guzzle 向 /oauth/token/ 发出请求作为登录请求,并看到连接被拒绝

我的自定义控制器中的代码是:

    public function login(Request $request)
    {
        $http = new \GuzzleHttp\Client();
        try {
            $response = $http->post("https://mixapp.test/oauth/token/", [
                'form_params' => [
                    'grant_type' => 'password',
                    'client_id' => config('services.passport.client_id'),
                    'client_secret' => config('services.passport.client_secret'),
                    'username' => $request->username,
                    'password' => $request->password,
                ]
            ]);
            return $response->getBody();
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
            if ($e->getCode() === 400) {
                return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
            } else if ($e->getCode() === 401) {
                return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
            }
            return response()->json('Something went wrong on the server.', $e->getCode());
        }
    }

而且错误似乎是:

local.ERROR:cURL 错误 7:无法连接到 mixapp.test 端口 443:连接被拒绝(请参阅http://curl.haxx.se/libcurl/c/libcurl-errors.html){“异常”:“[对象](GuzzleHttp\Exception\ConnectException(代码:0): cURL 错误 7:无法连接到 mixapp.test 端口 443:连接被拒绝

两者都在 https 上,我正在使用 laravel 代客。

【问题讨论】:

  • 您是否可以使用 curl 或某些 REST 扩展手动发布到该地址?
  • 是的,当我通过邮递员直接到达该端点时工作正常。

标签: php laravel curl guzzle laravel-valet


【解决方案1】:

实际上最终解决了这个问题。仔细想想,我只是从控制器调用同一个服务器,所以我将我的 guzzle 请求更改为:

$response = $http->post("https://127.0.0.1/oauth/token/", [...

感谢 SO 成为我的橡皮鸭 :)

【讨论】:

    猜你喜欢
    • 2017-12-25
    • 2011-08-05
    • 2022-10-23
    • 2014-07-28
    • 1970-01-01
    • 2020-07-14
    • 2014-10-09
    • 2018-06-21
    • 2017-04-23
    相关资源
    最近更新 更多