Your_Student
Your_Student

Reputation: 1

PHP Warning: curl_setopt(): supplied resource is not a valid cURL handle resource

I want to run my PHP file without any error but each time I faced some problem. I explained my problem below. Could any one can give me any suggestion so that I can solve it with a better way!

Thanks all who participate here.

PHP Warning:  curl_setopt(): supplied resource is not a valid cURL handle resource in p_detail.php on line 170

PHP Warning:  curl_setopt(): supplied resource is not a valid cURL handle resource in p_detail.php on line 171

PHP Warning:  curl_setopt(): supplied resource is not a valid cURL handle resource in p_detail.php on line 172

Here is my php file:


$banggoodAPI = new BanggoodAPI();


//Product Detail
$params = [
    'product_id'=>1588753,
];
$banggoodAPI->setParams($params);
$result = $banggoodAPI->getProductDetail();


echo '<pre>';
var_dump($result);

class BanggoodAPI {

    private $__apiKey = '***';
    private $__apiSecret = '**********';
    private $__domain = 'https://affapi.banggood.com/';

    private $__accessToken = '';

    private $__task = '' ;
    private $__method = 'GET';
    private $__params = array();
    private $__lang = 'en-GB';
    private $__currency = 'USD';

    private $__waitingTaskInfo = array();
    private $__ch = null;
    private $__curlExpireTime = 10;

    /**
     * @desc Construct
     * @access public
     */
    public function __construct(){
        $this->__ch = curl_init();
    }
    /**
     * @desc product/detail
     * @access public
     */
    public function getProductDetail() {
        $this->__task = 'product/detail';
        $this->__method = 'GET';
        $result = $this->__doRequest();

        return $result;
    }


    /**
     * @desc set params
     * @access public
     */
    public function setParams(Array $params) {
        if (!empty($params)) {
            $this->__params = $params;
        }
    }

    /**
     * @desc get access_token
     * @access private
     */
    private function __getAccessToken($useCache = true) {
        //if access_token is empty, send request to get accessToken
        if (empty($this->__accessToken)) {

            if (!empty($this->__task)) {

                $this->__waitingTaskInfo = array(
                    'task' => $this->__task,
                    'method' => $this->__method,
                    'params' => $this->__params,
                );
            }

            $this->__task = 'getAccessToken';
            $rand=rand();
            $time=time();
            $this->__params = [
                'api_key' => $this->__apiKey,
                'noncestr' => $rand,
                'timestamp' => $time,
            ];

            $preArr = array_merge($this->__params, ['api_secret' => $this->__apiSecret]);
            ksort($preArr);
            $signature=http_build_query($preArr);
            $this->__params['signature']=md5($signature);

            $this->__method = 'GET';

            $result = $this->__doRequest();
            if ($result['code'] == 200) {
                $this->__accessToken = $result['result']['access_token'];

                //resend request
                if (!empty($this->__waitingTaskInfo)) {

                    $this->__task = $this->__waitingTaskInfo['task'];
                    $this->__params = $this->__waitingTaskInfo['params'];
                    $this->__method = $this->__waitingTaskInfo['method'];

                    $this->__waitingTaskInfo = array();
                    return $this->__doRequest();
                }

            } else {

                $this->__requestError($result);
            }
        }
    }

    /**
     * @desc handle request error
     * @access private
     */
    private function __requestError($error) {

        var_dump($error);
        exit;
    }

    /**
     * @desc send api request
     * @access private
     */
    private function __doRequest() {

        if (empty($this->__params)) {
            $this->__requestError(array('params is empty'));
        }

        if ($this->__task != 'getAccessToken') {

            if (empty($this->__accessToken)) {
                $this->__getAccessToken();
            }

            //头部信息
            $header  = array(
                'access-token:'.$this->__accessToken,
            );

            if (empty($this->__params['lang']))
                $this->__params['lang'] = $this->__lang;

            if (empty($this->__params['currency']))
                $this->__params['currency'] = $this->__currency;
        }

        $apiUrl = $this->__domain . $this->__task;

        if ($this->__method == 'GET') {

            $quote = '?';
            foreach ($this->__params as $k => $v) {
                $apiUrl .= $quote . $k .'='. $v;
                $quote = '&';
            }
            $preStr = http_build_query($this->__params);
        }

        curl_setopt($this->__ch, CURLOPT_URL, $apiUrl );      //170 line
        curl_setopt($this->__ch, CURLOPT_HEADER, 0);          //171 line
        curl_setopt($this->__ch, CURLOPT_USERAGENT, $_SERVER ['HTTP_USER_AGENT']);   //172 line
        curl_setopt($this->__ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($this->__ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->__ch, CURLOPT_SSL_VERIFYPEER, false);

        if ($this->__method == 'POST') {
            curl_setopt($this->__ch, CURLOPT_POST, 1 );
            curl_setopt($this->__ch, CURLOPT_POSTFIELDS, http_build_query($this->__params));
        }

        if ($this->__curlExpireTime > 0) {
            curl_setopt($this->__ch, CURLOPT_TIMEOUT, $this->__curlExpireTime);
        }

        if ($header){
            curl_setopt($this->__ch, CURLOPT_HTTPHEADER, $header);
        }

        $result = curl_exec($this->__ch);
        if($error=curl_error($this->__ch)){
            die($error);
        }
        curl_close($this->__ch);
        $result = json_decode($result, true);

        return $result;
    }
}

?>

I want to run my PHP file without any error but each time I faced some problem. I explained my problem below. Could any one can give me any suggestion so that I can solve it with a better way!

Thanks all who participate here.

Upvotes: 0

Views: 1343

Answers (1)

IMSoP
IMSoP

Reputation: 97783

You are "closing" your curl handle after every request (curl_close($this->__ch);), but only "initializing" it ($this->__ch = curl_init();) once in the constructor.

Once it's been "closed", you can't re-use it for another request. Since you're setting all the options for each request anyway, you probably want to run curl_init() for every request, at the start of the __doRequest method.

Upvotes: 2

Related Questions