PHP接口

来源:互联网 发布:python class是什么 编辑:程序博客网 时间:2024/06/06 12:36
也可看http://www.cnblogs.com/jiqing9006/p/3949190.html进行参考

<?php
// echo md5("news1");
/*
查询
接口地址:http://apis.juhe.cn/mobile/get
支持格式:json/xml
请求方式:get
请求示例:http://localhost/day9.8/port.php?key=c7d81b6949462e2f1bb5fdf18dd1d006&id=用户id&dtype=xx
请求参数说明:
id int 是 用户id
key string 是 应用APPKEY(应用详细页查询)
dtype string 否 返回数据的格式,xml或json,默认json
返回参数说明:
error_code int 返回码
reason string 返回说明
result string 返回结果集
*/
// 模拟
// $key=c7d81b6949462e2f1bb5fdf18dd1d006;
// $id=1;
// 接收用户信息
$key=$_GET['key'];
$id=$_GET['id'];
// 如果用户写dtype接收值,没有默认json
$dtype=isset($_GET['dtype'])?$_GET['dtype']:'json';
// 服务器生成key值和用户key值对比
$app_key=md5("news$id");
if($app_key!=$key){
echo"非法请求";die;
}
// 生成接口
$pdo=newPDO("mysql:host=localhost;dbname=news","root","root");
$pdo->exec("set names utf8");
// 得到数据库的内容
$sql="select * from hao123";
$data=$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
if($dtype=='xml'){
// 生成xml格式数据
header("content-type:text/xml;charset=utf8");
echo"<?xml version='1.0' encoding='utf-8'?>";
echo"<news>";
foreach($dataas$key => $value){
echo"<new>";
echo"<title><![CDATA[{$value['link']}]]></title>";
echo"<link><![CDATA[{$value['title']}]]></link>";
echo"</new>";
}
echo"</news>";
}else{
// 生成json格式数据
header("content-type:text/html;charset=utf8");
echojson_encode($data,JSON_UNESCAPED_UNICODE);
}
?>
0 0
原创粉丝点击