【问题标题】:PHP Laravel BaseRepository CURL Error [ curl_setopt_array(): supplied resource is not a valid cURL handle resource ]PHP Laravel BaseRepository CURL 错误 [ curl_setopt_array(): 提供的资源不是有效的 cURL 句柄资源]
【发布时间】:2019-08-03 11:02:01
【问题描述】:

我在 Laravel 中制作了一个小而简单的 Web 应用程序,为此我使用 API 来获取数据并以用户友好的方式进行可视化。为了从 API 处理/获取数据,我在我的应用程序中创建了一个 BaseRepository,但是我收到了这个错误:"curl_setopt_array(): supplied resource is not a valid cURL handle resource"

这是它的样子

如果有人能提供帮助,那就太好了。

这是我的代码:

<?php

namespace App\Repositories;


// Class BaseRepository.

class BaseRepository
{

    public $curl = null;

    public function __construct()
    {
        $this->curl = curl_init();
    }

    public function getAccessToken()
    {

        curl_setopt_array($this->curl, array(
            CURLOPT_URL => "https://identity.vwgroup.io/oidc/v1/token",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id=MY_ID_GOES_HERE&client_secret=MY_CLIENT_SECRET",
            CURLOPT_HTTPHEADER => array(
                "Cache-Control: no-cache",
                "Content-Type: application/x-www-form-urlencoded",
                "Postman-Token: 84e72567-25d5-42ba-8a94-fe88e6bcc41d",
                "cache-control: no-cache",
            ),
        ));

        $response = curl_exec($this->curl);
        $err = curl_error($this->curl);

        curl_close($this->curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            // Convert JSON string to Object
            $responseObject = json_decode($response);
            $accessToken = $responseObject->access_token; // Access Object data


            return $accessToken;
        }
    }

    public function getCountries()
    {

        curl_setopt_array($this->curl, array(
            CURLOPT_URL => "https://api.productdata.vwgroup.com/v2/countries",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                "Accept: application/json",
                "Authorization: bearer " . $this->getAccessToken(),
                "Postman-Token: e6625e0a-fc50-4382-8812-8d151457dcab,67edecb8-64bd-4c93-9892-ec1061d5f210",
                "cache-control: no-cache,no-cache",
            ),
        ));

        $response = curl_exec($this->curl);
        $err = curl_error($this->curl);

        curl_close($this->curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            // Convert JSON string to Object
            $responseObject = json_decode($response, true);
            $data = array('data' => $responseObject['data']);
            return $data;
        }
    }

}

【问题讨论】:

  • 你如何调用这些方法?在每个方法的末尾加上curl_close($this-&gt;curl) 将使得不可能在同一个类实例的另一个方法中使用。
  • 啊,我现在明白了。您从getCountries() 拨打getAccessToken()。看到在共享句柄上使用curl_close() 的问题了吗?
  • 然后从我的控制器调用这些方法,然后将信息传递给我的视图

标签: php laravel api curl


【解决方案1】:

我的猜测是您的子存储库类之一正在定义 __construct() 方法,而您忘记在其中调用 parent::__construct(),因此永远不会调用 curl_init() 方法。

编辑:Phil 的评论是您问题的另一个非常可能的原因。在一种方法中调用 curl_close() 将阻止下一种方法使用共享 cURL init。

我真的建议将curl_init() 移动到每个存储库方法。您的请求不相关,并且可能共享不同的选项和配置。通过修改单个 cURL 实例,您可能会共享您不想要的选项。它还允许您在每个子存储库类中使用__construct(),而无需担心 BaseRepository 及其构造函数(您可以删除)。

【讨论】:

  • 谢谢它的工作只需将其放入函数curl_init()
猜你喜欢
  • 2014-05-03
  • 2012-09-05
  • 2015-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多