**********************************************************************************************
/*这是PHP创建的一个小接口*/
<?php
$mysqli = new mysqli('localhost', 'root', '123456','interfacetest');
mysqli_query($mysqli,"set names utf8");
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$output = array();
$user_by= $_POST['user_by'];
$uid = $_POST["uid"];
if (empty($user_by)) {
$output = array('data'=>NULL, 'info'=>'this is null!', 'stats'=>1);
exit(json_encode($output));
}
if ($user_by == 'get_userinfo') {//调用获取用户信息的接口
//查询数据库
$sql="select * from user WHERE user_id=$uid";
$result =$mysqli->query($sql);
$userInfo = $result -> fetch_row();
if($userInfo){//如果数据存在输出数据
$output = array(
'data' => array(
'userInfo' => $userInfo,
),
'stats'=>0
);
}else{
$output = array(
'data' => array(
'userInfo' => $userInfo,
),
'stats'=>1
);
}
exit(json_encode($output));//把结果反馈给客户端
}
$mysqli->close();
?>
**********************************************************************************
/*这是一个接口调用的方法(post)*/
$url = 'http://localhost/testmysql.php';
$sl_data=array(
'uid'=>1,
'user_by'=>'get_userinfo'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);//要访问的地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//执行结果是否被返回,0是返回,1是不返回
curl_setopt($ch, CURLOPT_POST, 1);// 发送一个常规的POST请求
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($sl_data));
$output = curl_exec($ch);//执行并获取数据
echo $output;
curl_close($ch);