-
-
/**
- * API介面服務端
- * site http://bbs.it-home.org
- *
- */
- 需要'conn.php';
- header('Content-Type:texttext /html;charset=utf-8');
-
- $action = $_GET['action'];
- switch ($action) {
-
- //註冊會員
- case"adduserinfo";
- $username = lib_replace_end_tag(trim($_GET['username']));
- $password2 = lib_replace_end_tag(trim($_GET['userpassword']));
- $password = md5("$password2" . ALL_PS);
- $email = lib_replace_end_tag(trim($_GET['email']));
-
- if ($username == '' || $password2 = = '' || $password == '') {
- $res = urlencode("參數錯誤");
- 退出(json_encode($res)); //有空資訊
- }
-
- $sql = "select username from `member` where username='$username'";
- $query = mysql_query($sql, $conn);
- $count = mysql_num_rows($sqlry) ;
-
- if ($count > 0) {
- exit(json_encode(1)); //回傳1表示註冊失敗
- } else {
-
- $addsql = "插入`member`(使用者名稱,密碼,email)values ('$username','$password','$email ')";
- mysql_query($addsql);
- 退出(json_encode(0)); //回傳0表示註冊成功
- }
- break;
-
- //查詢使用者資訊
- case"selectuserinfo";
- $username = lib_replace_end_tag($_GET['username'] );
- $sql = "從`member` 選擇id,使用者名稱,暱稱,手機,其中username='$username'";
- $query = mysql_query($sql, $conn);
- $row = mysql_fetch_array($query);
- foreach ($row as $key => $v) {
- $res[$key] = urlencode($v);
- }
- 退出(json_encode($res));
- break;
-
- // 會員登入
- case"userlogin";
- $username = lib_replace_end_tag($_GET['username']); $password2 = lib_replace_end_tag(trim($_GET['userpassword']));
- $password = md5("$password2" . ALL_PS);
- $sqluser = "從`member` 選擇ID、用戶名、密碼,其中username='" 。 $使用者名稱 . “'和密碼='”。 $密碼。 「'」;
- $queryuser = mysql_query($sqluser);
- $rowuser = mysql_fetch_array($queryuser);
- if ($rowuser && is_array($rowuser) && !emptyempty($rowuser)) {
- if ($rowuser['username'] == $username && $rowuser['password'] == $password ) {
- if ($rowuser['password'] == $password) {
- $res = urlencode("登入成功");
- 退出(json_encode($res));
- } else {
- $res = urlencode("密碼錯誤");
- 退出(json_encode ($res));
- }
- } else {
- $res = urlencode("使用者名稱不存在");
- 退出(json_encode($res));
- }
- }
- } else {
- $res = urlencode("使用者名稱密碼錯誤");
- 退出(json_encode($res));
- }
- /*
- * 0:表示登入成功,1:表示密碼錯誤,2:使用者名稱不存在,3:使用者名稱密碼錯誤
- */
- break;
-
- 預設:
- exit(json_encode(error));
- }
? >
複製程式碼
2、客戶範例:
-
-
/**
- * 客戶端呼叫API
- * site http://bbs.it-home.org
- */
- header('Content-Type:text/html;charset=utf - 8'); // 避免輸出亂碼
-
- function httpPost($url, $parms) {
- $url = $url . $參數;
- if (($ch = curl_init($url)) == false) {
- throw new Exception(sprintf("url %s 的curl_init 錯誤。", $url));
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- if (is_array($parms)) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data;'));
- }
- $postResult = @curl_exec($ch);
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- if ($postResult === false || $http_code != 200 || curl_errno($ch)) {
- $error = curl_error($ch);
- curl_close($ch);
- throw new Exception("HTTP POST FAILED:$error");
- } else {
- // $postResult=str_replace("xEFxBBxBF", '', $postResult);
- switch (curl_getinfo($ch, CURLINFO_CONTENT_TYPE)) {
- case 'application/json':
- $postResult = json_decode($postResult);
- 休息;
- }
- curl_close($ch);
- 回傳 $postResult;
- }
- }
-
- $postUrl = "http://pujia.test.com/api/server.php";
-
- $p=$_GET['p'];
- if ($p ==="selectuserinfo") {
-
- $username = $_GET['username'];
- $parms = "?action=selectuserinfo&username=" . $用戶名 . “”;
-
- } elseif ($p ==="adduserinfo") {
-
- $username = $_GET['username'];
- $userpassword = $_GET['userpassword'];
- $parms = "?action=adduserinfo&username=" . $用戶名 . “&用戶密碼=”。
- $userpassword = $_GET['userpassword'];
- $parms = "?action=userlogin&username=" . $用戶名 . “&用戶密碼=”。 //$parms
- $res = json_decode($res);
- print_r(urldecode(json_encode($res)));
- ?>
-
-
- 複製程式碼
-
-
- 以上就是今天php教程給出的範例程式碼,用php開發簡單的API接口,希望有所了解幫助。
程式設計師之家,專為心你每一天。
|