您好,欢迎访问一九零五行业门户网

php类 方法里调用curl(),不成功

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); } }

提示:warning: curl_setopt(): supplied argument is not a valid curl handle resource in c:\appserv\www\qq_api2.php on line 45
我是在方法里调用curl(),好像curl()只能在_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); } }

提示:warning: curl_setopt(): supplied argument is not a valid curl handle resource in c:\appserv\www\qq_api2.php on line 45
我是在方法里调用curl(),好像curl()只能在_construct()里面初始化吧,如果是为什么?
不好意思,封贴.乱试试出来了。
在类里的方法声明的变量$ch是局部的,把所有$ch地方改成$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); }
其它类似信息

推荐信息