Home > Backend Development > PHP Tutorial > php类 方法里调用curl(),不成功

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

WBOY
Release: 2016-06-06 20:34:04
Original
1481 people have browsed it

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


    }   
}
</code>
Copy after login
Copy after login

提示: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()里面初始化吧,如果是为什么?

回复内容:

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


    }   
}
</code>
Copy after login
Copy after login

提示: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就行了

<code>    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);
    }
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template