使用PHP写一个简易接口

  安装XAMPPCoda,创建本地服务器>>http://localhost/index.html

   

  左为XAMPP(Apache服务器,MySQL数据库,PHP) 右为Coda

  在Coda中创建登录界面(Get 和 Post)和Get,Post接受请求界面:

  使用PHP写一个简易接口_第1张图片

 1 
 2 
 3 <html >
 4     <head >
 5         <meta charset="utf-8"/>
 6         <title >My Webtitle>
 7     head>
 8     
 9 
10 
11     <body >
12         
13         <form method="post" action="http://localhost/post.php">
14 
15         
16 
18             <p align="center">用户名:<input type="text" name="username"> p>
19             <br/>
20             <p align="center">密 码:<input type="text" name="password"> p>
21             <br/>
22             <p align="center"><input type="submit" value="登录"/>p>
23         form>
24     body>
25 html>

 

  GET请求 -> http://localhost/get.php?username=zhangwei&password=123456

 1 php
 2     header("Content-type:text/html; charset=utf-8");
 3     
 4     // 获取form表单值
 5     $username = $_GET['username'];
 6     $password = $_GET['password'];
 7     
 8     // 判断form表单中key
 9     if(isset($_GET['username']) && isset($_GET['password'])){
10         
11         // 判断username和password
12         if($username == "zhangwei" && $password == "123456"){
13             
14             $result = array("success" => 1, "code" => 101, "data" => array("username" => $username, "password" => $password));    
15         
16         }else{
17             $result = array("success" => 0, "code" => 103, "data" => null);
18         }
19         
20     }else{
21         $result = array("success" => 0, "code" => 100, "data" => null);
22     }
23     // 将错误信息(数组)转换成json类型,返回前端
24     echo(json_encode($result));
25 ?>

 

   POST请求 -> http://localhost/post.php

 1 php
 2     header("Content-type:text/html; charset=utf-8");
 3     
 4     // 获取form表单值
 5     $username = $_POST['username'];
 6     $password = $_POST['password'];
 7     
 8     // 判断form表单中key
 9     if(isset($_POST['username']) && isset($_POST['password'])){
10         
11         // 判断username和password
12         if($username == "zhangwei" && $password == "123456"){
13             
14             $result = array("success" => 1, "code" => 101, "data" => array("username" => $username, "password" => $password));    
15         
16         }else{
17             $result = array("success" => 0, "code" => 103, "data" => null);
18         }
19         
20     }else{
21         $result = array("success" => 0, "code" => 100, "data" => null);
22     }
23     // 将错误信息(数组)转换成json类型,返回前端
24     echo(json_encode($result));
25 ?>

 

  在Xcode中进行解析:

 1     NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/post.php?username=zhangwei&password=123456"];
 2 /*  
 3     NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/post.php"];
 4 */
 5 
 6     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
 7 
 8     request.HTTPMethod = /* @"POST" */ @"GET";
 9 
10 /*
11     request.HTTPBody = [@"username=zhangwei&password=123456" dataUsingEncoding:NSUTF8StringEncoding];
12 */
13     // iOS 9 解析方法
14     NSURLSession *session = [NSURLSession sharedSession];
15 
16     NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
17         NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
18         NSLog(@"%@", dict);
19     }];
20     // 启动Task
21     [task resume];
22 
23 打印结果:
24 2016-04-20 21:50:40.227 GET[13393:312923] {
25     code = 101;                            
26     data =     {                           
27         password = 123456;                 
28         username = zhangwei;               
29     };                                     
30     success = 1;                           
31 }                                          

转载于:https://www.cnblogs.com/kriskee/p/5414556.html

你可能感兴趣的:(使用PHP写一个简易接口)