Failed to call curl () in php Class Method

Source: Internet
Author: User
{Code ...} tip: Warning: curl_setopt (): Warning: AppServwwwqq_api2.phponline45 I call curl () in the method, as if curl () can only be in _ construct...
class Qq_api {    private $list;    function __construct() {        $this->list['dsp_id'] = DSP_ID;        $this->list['token'] = TOKEN;    }    function curl_post($end = '') {$ch = curl_init(URL.$end);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    }    function sync() {        $this->curl_post('sync');        $arr = array();curl_setopt($ch, CURLOPT_POSTFIELDS,$arr);$result = curl_exec($ch);var_dump($result);    }   }

Tip:Warning: curl_setopt (): supplied argument is not a valid cURL handle resource in C: \ AppServ \ www \ qq_api2.php on line 45

I call curl () in the method. It seems that curl () can only be initialized in _ construct (). Why?

Reply content:
class Qq_api {    private $list;    function __construct() {        $this->list['dsp_id'] = DSP_ID;        $this->list['token'] = TOKEN;    }    function curl_post($end = '') {$ch = curl_init(URL.$end);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    }    function sync() {        $this->curl_post('sync');        $arr = array();curl_setopt($ch, CURLOPT_POSTFIELDS,$arr);$result = curl_exec($ch);var_dump($result);    }   }

Tip:Warning: curl_setopt (): supplied argument is not a valid cURL handle resource in C: \ AppServ \ www \ qq_api2.php on line 45

I call curl () in the method. It seems that curl () can only be initialized in _ construct (). Why?

Sorry, I tried to block it.

The variable $ ch declared by the method in the class is local. Change all $ ch to $ this-> ch.

    function curl_post($end = '') {$this->ch = curl_init(URL.$end);curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'POST');curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.