微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Symfony 客户端发出 localhost API 请求?

如何解决使用 Symfony 客户端发出 localhost API 请求?

我正在尝试使用 Symfony 5 向我使用 API 平台开发的 API 发出请求。

有问题的网址是 https://127.0.0.1:8000/api/cheeses

当我使用 Postman 对其进行 GET 请求时,一切正常。如果我要在浏览器中访问该 url,它会起作用。但是不可能用 Symfony 在 PHP 中发出请求。我是这样处理的:

use Symfony\Contracts\HttpClient\HttpClientInterface;

class AbstractService
{

   public function __construct(private HttpClientInterface $client){}

   protected function request(string $method,string $path,array $params = [])
    {

        $response = $this->client->request('GET','https://127.0.0.1:8000/api/cheeses',[
            'headers' => [
                'Content-Type' => 'application/json',]
        ]);

        dd(json_decode($response->getContent(),true,512,JSON_THROW_ON_ERROR));
    }

}

当我尝试循环时出现错误

“https://127.0.0.1:8000/api/cheeses”达到空闲超时。

但如果我尝试使用像 https://api.github.com/repos/guzzle/guzzle 这样的外部 URL,它会起作用。

我的 nelmio_cors 配置:

nelmio_cors:
    defaults:
        origin_regex: true
        allow_origin: ['*']
        allow_methods: ['GET','OPTIONS','POST','PUT','PATCH','DELETE']
        allow_headers: ['Content-Type','Authorization']
        expose_headers: ['Link']
        max_age: 3600
    paths:
        '^/': null

有什么想法吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。