Exemplo n.º 1
0
Arquivo: api.php Projeto: muqidi/PHP
/**
 *  返回结果集
 *
 *  @param   mixed      $info       返回的有效数据集或是错误说明
 *  @param   string     $msg        为空或是错误类型代号
 *  @param   string     $result     请求成功或是失败的标识
 *  @param   int        $post       1为xml方式,2为json方式
 *
 */
function data_back($info, $msg = '', $post, $result = 'success')
{
    /* 分为xml和json两种方式 */
    $data_arr = array('result' => $result, 'msg' => $msg, 'info' => $info);
    $data_arr = to_utf8_iconv($data_arr);
    //确保传递的编码为UTF-8
    if ($post == 1) {
        /* xml方式 */
        if (class_exists('DOMDocument')) {
            $doc = new DOMDocument('1.0', 'UTF-8');
            $doc->formatOutput = true;
            $shopex = $doc->createElement('shopex');
            $doc->appendChild($shopex);
            $result = $doc->createElement('result');
            $shopex->appendChild($result);
            $result->appendChild($doc->createCDATASection($data_arr['result']));
            $msg = $doc->createElement('msg');
            $shopex->appendChild($msg);
            $msg->appendChild($doc->createCDATASection($data_arr['msg']));
            $info = $doc->createElement('info');
            $shopex->appendChild($info);
            create_tree($doc, $info, $data_arr['info']);
            die($doc->saveXML());
        }
        die('<?xml version="1.0" encoding="UTF-8"?>' . array2xml($data_arr));
    } else {
        /* json方式 */
        $json = new JSON();
        die($json->encode($data_arr));
        //把生成的返回字符串打印出来
    }
}
Exemplo n.º 2
0
/**
 * 循环转码成utf8内容
 *
 * @param string $str
 * @return string
 */
function to_utf8_iconv($str)
{
    if (EC_CHARSET != 'utf-8')
    {
        if (is_string($str))
        {
            return ecs_iconv(EC_CHARSET, 'utf-8', $str);
        }
        elseif (is_array($str))
        {
            foreach ($str as $key => $value)
            {
                $str[$key] = to_utf8_iconv($value);
            }
            return $str;
        }
        elseif (is_object($str))
        {
            foreach ($str as $key => $value)
            {
                $str->$key = to_utf8_iconv($value);
            }
            return $str;
        }
        else
        {
            return $str;
        }
    }
    return $str;
}