码迷,mamicode.com
首页 > Web开发 > 详细

PHP 开放JSON格式接口实例

时间:2015-07-02 09:52:13      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:

转化JSON文件

<?php
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
    static $recursive_counter = 0;
    if (++$recursive_counter > 1000) {
        die(possible deep recursion attack);
    }
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $function, $apply_to_keys_also);
        } else {
            $array[$key] = $function($value);
        }

        if ($apply_to_keys_also && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
    $recursive_counter--;
}
function JSON($array) {
    arrayRecursive($array, urlencode, true);
    $json = json_encode($array);
    return urldecode($json);
}
/**
     * Ajax方式返回数据到客户端
     * @access protected
     * @param mixed $data 要返回的数据
     * @param String $type AJAX返回数据格式
     * @param int $json_option 传递给json_encode的option参数
     * @return void
     */
function ajaxReturn($data,$type=‘‘,$json_option=0) {
    if(empty($type)) $type  =   C(DEFAULT_AJAX_RETURN);
    switch (strtoupper($type)){
        case JSON :
            // 返回JSON数据格式到客户端 包含状态信息
            header(Content-Type:application/json; charset=utf-8);
            exit(json_encode($data,$json_option));
        case XML  :
            // 返回xml格式数据
            header(Content-Type:text/xml; charset=utf-8);
            exit(xml_encode($data));
        case JSONP:
            // 返回JSON数据格式到客户端 包含状态信息
            header(Content-Type:application/json; charset=utf-8);
            $handler  =   isset($_GET[C(VAR_JSONP_HANDLER)]) ? $_GET[C(VAR_JSONP_HANDLER)] : C(DEFAULT_JSONP_HANDLER);
            exit($handler.(.json_encode($data,$json_option).););  
        case EVAL :
            // 返回可执行的js脚本
            header(Content-Type:text/html; charset=utf-8);
            exit($data);            
        default     :
            // 用于扩展其他返回格式数据
            Hook::listen(ajax_return,$data);
    }
}
?>

 

引入json.php文件

连接mysql查询数据

<?php
//引入json.php文件
require_once(json.php); 

$mysql_server_name="servername";
$mysql_username="root";
$mysql_password="password";
$mysql_database="database";
//连接到数据库
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
//编码转化
mysql_query("set names ‘utf8‘");
if(!$conn)
{
    //诊断连接错误
    die("could not connect to the database:</br>".mysql_error());
}
$mysql_select=mysql_select_db($mysql_database,$conn);
if(!$mysql_select)
{
    die("could not to the database:</br>".mysql_error());
}
//从表中提取信息的sql语句
$strsql="select * from table";
//执行sql查询
$result=mysql_query($strsql);

定义和用法

mysql_fetch_array() 函数从结果集中取得一行作为关联数组,或数字数组,或二者兼有

返回根据从结果集取得的行生成的数组,如果没有更多行则返回 false。

语法

mysql_fetch_array(data,array_type)
参数描述
data 可选。规定要使用的数据指针。该数据指针是 mysql_query() 函数产生的结果。
array_type

可选。规定返回哪种结果。可能的值:

  • MYSQL_ASSOC - 关联数组
  • MYSQL_NUM - 数字数组
  • MYSQL_BOTH - 默认。同时产生关联和数字数组

转化为array

$departList=array();
while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
    $departList[]=$row;
}
echo JSON($departList);
//释放资源
mysql_free_result($userresult);
mysql_free_result($deptresult);
//关闭连接
mysql_close($conn);

?>

 

PHP 开放JSON格式接口实例

标签:

原文地址:http://www.cnblogs.com/bmbh/p/4615155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!