Home > Backend Development > PHP Tutorial > CI 问题 curl

CI 问题 curl

WBOY
Release: 2016-06-06 20:35:02
Original
1010 people have browsed it

Library

<code>public function doRequest($url, $para)
    {

        do {

            if(extension_loaded('curl')) {

                $ch = curl_init();

                $options = array(
                    CURLOPT_URL     => $url,
                    CURLOPT_HEADER  => 0,
                    CURLOPT_POST    => 1,
                    CURLOPT_POSTFIELDS      => $para,
                    CURLOPT_SSL_VERIFYPEER  => 0,
                    CURLOPT_SSL_VERIFYHOST  => 0,
                    CURLOPT_SSLVERSION      => 1,
                    CURLOPT_CONNECTTIMEOUT  => 5,
                    CURLOPT_USERAGENT       => $_SERVER['HTTP_USER_AGENT'],
                    );

                curl_setopt_array($ch, $options);

                $data = curl_exec($ch);
                $info = curl_getinfo($ch);
                $error = curl_error($ch);
                curl_close($ch);

                if(get_magic_quotes_gpc()) {

                    $data = stripslashes($data);
                }

                $decode = json_decode($data, true);

                if(json_last_error() === JSON_ERROR_NONE) {

                    $err =  array(
                        'status'    => 'success',
                        'results'   => $info,
                    );
                }
                else
                {
                    $err = array(
                        'status'    => 'failed',
                        'results'   => '返回值非JSON 类型',
                        );
                }



            } else {

                $err = array(
                    'status'    => 'failed',
                    'results'   => '请开启PHP Curl 拓展',
                    );

            }

            return $err;

        } while(0);
    }
</code>
Copy after login
Copy after login

Controller

<code>public function how_old()
    {
        $url = config_item('how_old_url');

        $data = array(
            'isTest'   => false,
            'faceName' => 'Test',
            'faceUrl'  => 'http://www.sinaimg.cn/dy/slidenews/4_img/2015_12/704_1579346_230297.jpg',
            );

        $this->load->library('cuiqg');

        var_dump( $this->cuiqg->doRequest($url, $data))];
    }
</code>
Copy after login
Copy after login

Browser
CI 问题 curl

奇怪 没有任何 打印 他是怎么显示出来的 ,在一个 gettype() doRequest() $data 是bool 类型的 怎么处理, 还有就是这些转义字符怎么去掉 ~_~||

回复内容:

Library

<code>public function doRequest($url, $para)
    {

        do {

            if(extension_loaded('curl')) {

                $ch = curl_init();

                $options = array(
                    CURLOPT_URL     => $url,
                    CURLOPT_HEADER  => 0,
                    CURLOPT_POST    => 1,
                    CURLOPT_POSTFIELDS      => $para,
                    CURLOPT_SSL_VERIFYPEER  => 0,
                    CURLOPT_SSL_VERIFYHOST  => 0,
                    CURLOPT_SSLVERSION      => 1,
                    CURLOPT_CONNECTTIMEOUT  => 5,
                    CURLOPT_USERAGENT       => $_SERVER['HTTP_USER_AGENT'],
                    );

                curl_setopt_array($ch, $options);

                $data = curl_exec($ch);
                $info = curl_getinfo($ch);
                $error = curl_error($ch);
                curl_close($ch);

                if(get_magic_quotes_gpc()) {

                    $data = stripslashes($data);
                }

                $decode = json_decode($data, true);

                if(json_last_error() === JSON_ERROR_NONE) {

                    $err =  array(
                        'status'    => 'success',
                        'results'   => $info,
                    );
                }
                else
                {
                    $err = array(
                        'status'    => 'failed',
                        'results'   => '返回值非JSON 类型',
                        );
                }



            } else {

                $err = array(
                    'status'    => 'failed',
                    'results'   => '请开启PHP Curl 拓展',
                    );

            }

            return $err;

        } while(0);
    }
</code>
Copy after login
Copy after login

Controller

<code>public function how_old()
    {
        $url = config_item('how_old_url');

        $data = array(
            'isTest'   => false,
            'faceName' => 'Test',
            'faceUrl'  => 'http://www.sinaimg.cn/dy/slidenews/4_img/2015_12/704_1579346_230297.jpg',
            );

        $this->load->library('cuiqg');

        var_dump( $this->cuiqg->doRequest($url, $data))];
    }
</code>
Copy after login
Copy after login

Browser
CI 问题 curl

奇怪 没有任何 打印 他是怎么显示出来的 ,在一个 gettype() doRequest() $data 是bool 类型的 怎么处理, 还有就是这些转义字符怎么去掉 ~_~||

<code>$url="http://how-old.net/Home/Analyze";
 $data = array(
            'isTest'   => false,
            'faceName' => 'Test',
            'faceUrl'  => 'http://www.sinaimg.cn/dy/slidenews/4_img/2015_12/704_1579346_230297.jpg',
            );

 function post( $url, $params, $cookie ="")
    {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

$result =  post($url,$data);
var_dump(json_decode(json_decode($result)));
</code>
Copy after login
Related labels:
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