首页 > 后端开发 > php教程 > CI 问题 curl

CI 问题 curl

WBOY
发布: 2016-06-06 20:35:02
原创
1011 人浏览过

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>
登录后复制
登录后复制

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>
登录后复制
登录后复制

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>
登录后复制
登录后复制

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>
登录后复制
登录后复制

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>
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板