【问题标题】:cURL error 3: <url> malformed - Laravel 5.6cURL 错误 3:<url> 格式错误 - Laravel 5.6
【发布时间】:2021-04-24 15:30:46
【问题描述】:

在发送 API 发布请求时,它给了我一个 curl 错误,请查看下面的详细信息,并请建议我该如何解决这个问题。

cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-
errors.html)

.env

APP_URL=http://acpapi.staging

注册控制器

$user = $this->create($request->all());
            $http = new Client;
            // return $http->post(env('APP_URL'));
            $response = $http->post(env('APP_URL') . '/oauth/token', [
                'form_params' => [
                    'grant_type' => 'password',
                    'client_id' => env('PASSWORD_CLIENT_ID'),
                    'client_secret' => env('PASSWORD_CLIENT_SECRET'),
                    'username' => $request->get('email'),
                    'password' => $request->get('password'),
                    'remember' => false,
                    'scope' => '',
                ],
            ]);
return $this->successResponse([
                'token_data' => json_decode($response->getBody()),
                'user' => [
                    'email' => $user->email
                ]
            ]);

错误

"cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)"

【问题讨论】:

  • 您尝试的 URL 的值是多少?你 100% 肯定它是正确的吗?
  • 尝试使用dd(env('APP_URL') . '/oauth/token'); 并验证您是否得到了预期的结果。
  • 最好不要在配置文件之外调用env ...使用配置系统中的值config('app.url') ...如果配置被缓存,.env文件没有被读取并且对env的调用将返回nulllaravel.com/docs/5.6/configuration#configuration-caching
  • 它正在返回"/oauth/token"

标签: laravel curl laravel-5.6 guzzle


【解决方案1】:

就我而言,我错误地将我的环境文件命名为“env”

确保它是“.env”

【讨论】:

    【解决方案2】:

    您可以尝试使用 php artisan config:clear 清除配置缓存,如果它之前被缓存并且您更改了值,这可能是原因。

    您也可以尝试使用this 包进行内部请求。

    【讨论】:

      【解决方案3】:

      不使用

      php artisan config:cache

      您可以参考这里了解更多信息:Is there a way to tell curl to not use cache

      【讨论】:

        猜你喜欢
        • 2018-11-03
        • 2016-06-28
        • 2015-06-25
        • 1970-01-01
        • 2019-09-01
        • 1970-01-01
        • 2019-01-14
        • 2019-06-03
        • 1970-01-01
        相关资源
        最近更新 更多