php写的json服务端接口以及用php做的调用页面

用php写的服务端,实现json传参数查询mysl的数据,mysql的简单查询与json对比,用的是dedecms的数据库,查询的是dede_area表,http://localhost:8080/test/index.php?index=5&count=5 这是服务端的接口,页面效果如下:



  <head>
<meta charset="utf-8">
</head>
nihao 
<hr>
<?php
require("db_config.php");



// array for JSON response
$response = array(); //定义数组存放sql查询值
 
$count=$_GET['count'];
$index=$_GET['index'];
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ; //连接数据库   
mysql_query("set names 'utf8'"); //数据库输出编码 应该与你的数据库编码保持一致.建议用UTF-8 国际标准编码.  
mysql_select_db($mysql_database); //打开数据库   
$sql ="select * from dede_area order by id desc limit $index,$count"   ; //SQL语句   
$result = mysql_query($sql,$conn); //查询 




//这里用mysql_fetch_assoc 代替mysql_fetch_array,就可以让结果集带数组功能
while($row = mysql_fetch_assoc($result))   
{   
//排版代码
echo "<div style=\"height:24px; line-height:24px; font-weight:bold;\">"; //排版代码   
echo $row['id']."   ".$row['name'] ."   ".$row['reid']."   ".$row['disorder']. "<br/>";   
echo "</div>";   

//把数组交给$response
$response[]=$row; 



// echoing JSON response
// $response= json_encode($response);
// echo json_encode($response ); 
// php5.4才有此功能
echo json_encode($response, JSON_UNESCAPED_UNICODE); 


 
mysql_close();
?>

接下来是调用接口的php页面,当然也可以用asp.net,android或者其他后台语言来写,我h用的是php写的页面,文件名是index2.php,跟服务端以示区别,ttp://localhost:8080/test/index2.php,内容和服务端展现的是一样的代码如下:

<?php
 
$handle = fopen("http://192.168.0.45:8080/test/index.php?index=5&count=5","rb");
 
$content = "";
while (!feof($handle)) {
    $content .= fread($handle, 10000);
}
fclose($handle);


echo $content;


 

?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值