PHP Development API Interface and use
Service side
<?PhpRequire ' conn.php ';Header (' Content-type:text/html;charset=utf-8 ');$action =$_get[' action '];Switch ($action) {//Registered MembersCase "Adduserinfo";$username = Lib_replace_end_tag (Trim$_get[' username ']));$password 2 = Lib_replace_end_tag (Trim$_get[' UserPassword ']));$password =MD5 ("$password 2 ".ALL_PS);$email = Lib_replace_end_tag (Trim$_get[' email ']));if ($username = = "| |$password 2 = = "| |$password = = ") {$res =UrlEncode ("Wrong parameter");Exit (Json_encode ($res));//Free information}$sql = "Select username from ' member ' where Username= '$username ' ";$query =mysql_query ($sql,$conn);$count =Mysql_num_rows ($query);if ($count > 0) {Exit (Json_encode (1));//Return 1 indicates registration failed}Else{$addsql = "INSERT INTO ' member ' (Username,password,email) VALUES ('$username ', '$password ', '$email ') ";mysql_query ($addsql);Exit (Json_encode (0));//Returns 0 indicates successful registration}Break;//Querying user InformationCase "Selectuserinfo";$username = Lib_replace_end_tag ($_get[' username ']);$sql = "Select Id,username,nickname,mobile from ' member ' where Username= '$username ' ";$query =mysql_query ($sql,$conn);$row =Mysql_fetch_array ($query);foreach ($rowAs$key =$v) {$res [$key] =UrlEncode ($v); }Exit (Json_encode ($res));Break;//Member LoginCase "Userlogin";$username = Lib_replace_end_tag ($_get[' username ']);$password 2 = Lib_replace_end_tag (Trim$_get[' UserPassword ']));$password =MD5 ("$password 2 ".ALL_PS);$sqluser = "Select Id,username,password from ' member ' where Username= '".$username. "' and password= '".$password. "‘";$queryuser =mysql_query ($sqluser);$rowuser =Mysql_fetch_array ($queryuser);if ($rowuser &&Is_array ($rowuser) &&!Empty$rowuser)) {if ($rowuser [' username '] = =$username &&$rowuser [' password '] = =$password) {if ($rowuser [' password '] = =$password) {$res =UrlEncode ("Login Successful");Exit (Json_encode ($res)); }Else{$res =UrlEncode ("Bad password");Exit (Json_encode ($res)); } }Else{$res =urlencode ("user name does not exist" exit (Json_encode ( $res else { $res = urlencode ("Username password error" exit (Json_encode ( $res /* * 0: Indicates a successful login, 1: Password error, 2: Username does not exist, 3: User name password is incorrect */breakdefault: exit (Json_ Encode (error));}?
Client
<?PhpHeader (' Content-type:text/html;charset=utf-8 ');//Avoid garbled outputfunction HttpPost ($url,$parms) {$url =$url.$parms;if (($ch = Curl_init ($url)) = =False) {ThrowNewException (sprintf ("Curl_init error for URL%s.",$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);ThrowNewException ("HTTP POST FAILED:$error "); }Else{//$postResult =str_replace ("\XEF\XBB\XBF", ", $postResult);Switch (Curl_getinfo ($ch,Curlinfo_content_type)) {Case ' Application/json ':$postResult = Json_decode ($postResult);Break; } curl_close ($ch);Return$postResult; }}$POSTURL = "http://pujia.test.com/api/server.php";$p =$_get[' P '];if ($p = = "Selectuserinfo") {$username =$_get[' username '];$parms = "? Action=selectuserinfo&username=".$username. "";}ElseIf ($p = = "Adduserinfo") {$username =$_get[' username '];$userpassword =$_get[' UserPassword '];$parms = "? Action=adduserinfo&username=".$username. "&userpassword=".$userpassword. "";}ElseIf ($p = = "Userlogin") {$username =$_get[' username ' $userpassword = $_get[' UserPassword ' ]; $parms = "? Action=userlogin&username=". $username. "&userpassword=". $userpassword. ";} $res = HttpPost ( $postUrl, $parms); // $parms $res = Json_ Decode ( $res urldecode (Json_encode ( $res )));?
Manuscripts: Diligent Learning qkxue.net
Extended reading:
PHP Development API Interface and use
Http://qkxue.net/info/29563/PHP-API
PHP Development API Interface and use