分享

php开发API接口的代码案例

 A芝兰之室 2017-10-14
  1. <?php  
  2.   
  3. require 'conn.php';  
  4. header('Content-Type:text/html;charset=utf-8');  
  5.   
  6. $action = $_GET['action'];  
  7. switch ($action) {  
  8.   
  9.     //注册会员  
  10.     case"adduserinfo";  
  11.         $username = lib_replace_end_tag(trim($_GET['username']));  
  12.         $password2 = lib_replace_end_tag(trim($_GET['userpassword']));  
  13.         $password = md5("$password2" . ALL_PS);  
  14.         $email = lib_replace_end_tag(trim($_GET['email']));  
  15.   
  16.         if ($username == '' || $password2 == '' || $password == '') {  
  17.             $res = urlencode("参数有误");  
  18.             exit(json_encode($res)); //有空信息  
  19.         }  
  20.   
  21.         $sql = "select username from `member` where username='$username'";  
  22.         $query = mysql_query($sql, $conn);  
  23.         $count = mysql_num_rows($query);  
  24.   
  25.         if ($count > 0) {  
  26.             exit(json_encode(1)); //返回1表示注册失败  
  27.         } else {  
  28.   
  29.             $addsql = "insert into `member` (username,password,email) values ('$username','$password','$email')";  
  30.             mysql_query($addsql);  
  31.             exit(json_encode(0)); //返回0表示注册成功  
  32.         }  
  33.         break;  
  34.   
  35.   
  36.     //查询用户信息  
  37.     case"selectuserinfo";  
  38.         $username = lib_replace_end_tag($_GET['username']);  
  39.         $sql = "select id,username,nickname,mobile from `member` where username='$username'";  
  40.         $query = mysql_query($sql, $conn);  
  41.         $row = mysql_fetch_array($query);  
  42.         foreach ($row as $key => $v) {  
  43.             $res[$key] = urlencode($v);  
  44.         }  
  45.         exit(json_encode($res));  
  46.         break;  
  47.   
  48.   
  49.     //会员登录  
  50.     case"userlogin";  
  51.         $username = lib_replace_end_tag($_GET['username']);  
  52.         $password2 = lib_replace_end_tag(trim($_GET['userpassword']));  
  53.         $password = md5("$password2" . ALL_PS);  
  54.         $sqluser = "select id,username,password from `member` where username='" . $username . "' and password='" . $password . "'";  
  55.         $queryuser = mysql_query($sqluser);  
  56.         $rowuser = mysql_fetch_array($queryuser);  
  57.         if ($rowuser && is_array($rowuser) && !empty($rowuser)) {  
  58.             if ($rowuser['username'] == $username && $rowuser['password'] == $password) {  
  59.                 if ($rowuser['password'] == $password) {  
  60.                     $res = urlencode("登录成功");  
  61.                     exit(json_encode($res));  
  62.                 } else {  
  63.                     $res = urlencode("密码错误");  
  64.                     exit(json_encode($res));  
  65.                 }  
  66.             } else {  
  67.                 $res = urlencode("用户名不存在");  
  68.                 exit(json_encode($res));  
  69.             }  
  70.         } else {  
  71.             $res = urlencode("用户名密码错误");  
  72.             exit(json_encode($res));  
  73.         }  
  74.         /* 
  75.          * 0:表示登录成功,1:表示密码错误,2:用户名不存在,3:用户名密码错误 
  76.          */  
  77.         break;  
  78.   
  79.     default:  
  80.         exit(json_encode(error));  
  81. }  
  82. ?>  

客户端例子:
  1. <?php  
  2.   
  3. header('Content-Type:text/html;charset=utf-8'); //避免输出乱码  
  4.   
  5. function httpPost($url, $parms) {  
  6.     $url = $url . $parms;  
  7.     if (($ch = curl_init($url)) == false) {  
  8.         throw new Exception(sprintf("curl_init error for url %s.", $url));  
  9.     }  
  10.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  11.     curl_setopt($ch, CURLOPT_HEADER, 0);  
  12.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);  
  13.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
  14.     if (is_array($parms)) {  
  15.         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data;'));  
  16.     }  
  17.     $postResult = @curl_exec($ch);  
  18.     $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
  19.     if ($postResult === false || $http_code != 200 || curl_errno($ch)) {  
  20.         $error = curl_error($ch);  
  21.         curl_close($ch);  
  22.         throw new Exception("HTTP POST FAILED:$error");  
  23.     } else {  
  24.         // $postResult=str_replace("\xEF\xBB\xBF", '', $postResult);  
  25.         switch (curl_getinfo($ch, CURLINFO_CONTENT_TYPE)) {  
  26.             case 'application/json':  
  27.                 $postResult = json_decode($postResult);  
  28.                 break;  
  29.         }  
  30.         curl_close($ch);  
  31.         return $postResult;  
  32.     }  
  33. }  
  34.   
  35. $postUrl = "http://pujia./api/server.php";  
  36.   
  37. $p=$_GET['p'];  
  38. if ($p =="selectuserinfo") {  
  39.   
  40.     $username = $_GET['username'];  
  41.     $parms = "?action=selectuserinfo&username=" . $username . "";  
  42.   
  43. } elseif ($p =="adduserinfo") {  
  44.   
  45.     $username = $_GET['username'];  
  46.     $userpassword = $_GET['userpassword'];  
  47.     $parms = "?action=adduserinfo&username=" . $username . "&userpassword=" . $userpassword . "";  
  48.   
  49. } elseif ($p =="userlogin") {  
  50.     $username = $_GET['username'];  
  51.     $userpassword = $_GET['userpassword'];  
  52.     $parms = "?action=userlogin&username=" . $username . "&userpassword=" . $userpassword . "";  
  53.   
  54. }  
  55. $res = httpPost($postUrl, $parms); //$parms  
  56. $res = json_decode($res);  
  57. print_r(urldecode(json_encode($res)));  
  58. ?>  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多