PHP Implements JSONP Data interface

Source: Internet
Author: User

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677787980818283 <?php/** * @author zzclslove * @abstract php实现jsonp接口 * @description 在控制器层直接使用 ControllerTool::json_report($data, ‘GBK‘, $jsonp); * 即可向前台js返回数据*/classControllerTool    /**    * @abstract 将数据转换为json字符串    * @param array $data 需要输出的数据    * @param string $in_charset 输入数据的字符集,默认为GBK    * @param string $var 以jsonp格式赋值变量为$var    */    publicstaticfunctionjson($data$in_charset‘GBK‘$var = null)    {        if(!self::belongToUTF8($in_charset))        {            $data_utf8= self::iconv_object($in_charset‘UTF-8‘$data);        }        if(!empty($var))        {            echo"var ".$var"=";        }        returnjson_encode($data_utf8);    }    /**     * @abstract 将数据以json格式输出     * @param array $data 需要输出的数据     * @param string $in_charset 输入数据的字符集,默认为GBK     * @param string $var 以jsonp格式赋值变量为$var输出    */    publicstaticfunctionecho_json($data$in_charset‘GBK‘$var= null)    {        echoself::json($data$in_charset$var);    }                        publicstaticfunction echo_parent_callback_json($data$in_charset‘GBK‘$parent_callback= null)    {        echo‘<script>try{document.domain="webdev.com";}catch(e){}‘                .‘parent.‘.$parent_callback.‘(‘.self::json($data$in_charset).‘)‘                .‘</script>‘;    }    /**     * @abstract json格式报告结果     * @param array $data 数据正文     * @param string 数据正文的字符集     * @param string $var 以jsonp格式赋值变量为$var输出    */    publicstaticfunctionjson_report($dataarray(), $in_charset‘GBK‘$var= null, $parent_callback= null)    {        $json_dataarray();        $json_data[‘data‘] = $data;        if(!empty($parent_callback))        {            self::echo_parent_callback_json($json_data$in_charset$parent_callback);        }        else        {            self::echo_json($json_data$in_charset$var);        }    }        privatestaticfunctionbelongToUTF8($charset)    {          $utf8array(‘utf8‘‘UTF-8‘);          returnin_array($charset$utf8);    }   /**     * 对象字符集转换     * @param string $in_charset 输入字符集     * @param string $out_charset 输出字符集     * @param mixed $in_obj 输入对象     */    publicstaticfunctioniconv_object($in_charset$out_charset$in_obj)   {        $out_objarray();        self::do_iconv_object($in_charset$out_charset, &$in_obj, &$out_obj);        return$out_obj;   }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.