CURL 运用

CURL  POST请求

/**
     * POST请求数据工具函数
     * @param $url
     * @param $data
     * @return mixed
     */
    public function curlPost($url, $data)
    {
        $ch = curl_init();
        //地址
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        //禁用后cURL将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //禁用后cURL将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            //用来指定连接端口
            curl_setopt($ch, CURLOPT_POST, 1);
            if (is_array($data)) {
                //全部数据使用HTTP协议中的"POST"操作来发送。 key=>value 形式
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
            } else {
                //不需要解析直接用
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            }
        }
        //在启用CURLOPT_RETURNTRANSFER的时候,返回原生的(Raw)输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($ch);

        curl_close($ch);
        return $result;
    }

  CURL  GET请求

 

 /**
     * GET请求数据工具函数
     * @param $https
     * @return bool|mixed
     */
    public function curlGet($https)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $https);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return false;
        }
        curl_close($ch);
        return $tmpInfo;
    }

  

json字符串转换 

json_encode( )
json_decode( )
posted @ 2017-11-29 15:14  三七、  阅读(121)  评论(0编辑  收藏  举报