【问题标题】:Retrieve data from external api with Laravel使用 Laravel 从外部 api 检索数据
【发布时间】:2022-01-25 20:02:12
【问题描述】:

`我正在尝试使用 laravel 从外部 api 检索一些数据: 这是我的控制器

使用 Illuminate\Http\Request; 使用 Illuminate\Support\Facades\Http;

public function netvigie()  
    {
        $response = Http::get('https://extranet.netvigie.com/fr/api/{token}/availability/{id}/getAnomaliesCount?format=json');
        $data = json_decode($response->body(), true);
        dd($data);
        return view('ControlmAPI.netvigie');
    }

But i have curl error 28 while it works in Postman. We have only 2 parameters which are token and product id that we enter directly in the url.

Someone can help me please ?

【问题讨论】:

    标签: php laravel api


    【解决方案1】:

    你能写出完整的错误吗?

    用 Guzzle 试试:

    $client->request('GET', 'example.com/example', ['connect_timeout' => 3.14]);//3.14 is seconds, try to increase it 
    

    如果仍然给您带来问题,请尝试增加

    1. read_timeout
    2. 超时

    这些都是 Guzzle 的请求选项

    【讨论】:

    • "cURL 错误 28:无法连接到 extranet.netvigie.com 端口 443:超时" 好的,我将尝试使用 guzzle
    【解决方案2】:

    您是否尝试为通话设置超时?

    Http::timeout(3)->get(...);
    

    您可以在这里阅读更多内容https://laravel.com/docs/8.x/http-client#timeout

    【讨论】:

    • 您好,谢谢。我尝试增加时间,但仍然出现同样的错误,无法理解它在 Postman 上完美运行
    • API 是否要求您将标头设置为 application/json ?您在 Postman 上明确将标头设置为 application/json 吗?如果是的话,你有没有试过这个 Http::asJson()->get(...)
    【解决方案3】:

    这就是我尝试 dd 时所拥有的:

    public function netvigie()
           {
            return Http::dd()->get('https://extranet.netvigie.com/fr/api/{token}/availability/id/getStatusDetails?format=json');
           }
    

    回应: response

    它与邮递员一起工作,但与 laravel 我有“cURL 错误 28:无法连接到 extranet.netvigie.com 端口 443:超时”。我试图增加时间限制,但没有奏效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 2021-09-02
      • 1970-01-01
      相关资源
      最近更新 更多