How to Use PHP to write the APP Interface Details, phpapp Interface Details
Build a PHP Environment
Because my computer is Mac, we recommend that you use mamp pro, but it is a paid version. I believe you will have a solution if you don't want to spend any money!
Mamp pro is an integrated environment software that already has apache, mysql, and php. It has an environment for running and parsing PHP!
If you do not want to use the integrated environment, you can build it on your own. Mac comes with an apache and PHP environment. You only need to configure it a little and then install mysql!
Let's take a look at the simple version configuration: This article on configuring the PHP environment for Mac.
Start the server
Here we use mamp pro software as an example to describe how to start the server. For example, assume that the service address is www.api.com, and then associate the project directory with the service address, as shown in the circle in the lower right corner of the figure:
After the environment is started, you can directly enter www.api.com in the browser to run it!
Start GET Interface
After our empty project is run for the first time, the index. php file is automatically generated. we delete the content and modify it as follows:
<? Php $ data = array ('tid' => 100, 'name' => 'guy's technical blog ', 'SITE' => 'www .huangyibiao.com '); $ response = array ('code' => 200, 'message' => 'Success for request', 'data' => $ data,); echojson_encode ($ response );
The structure here is the most common returned data structure in daily App development, right? Generally, they are status codes, status information, and client business data.
PHP is actually awesome. After declaring the arrayjson_encode The function can output data in json format!
Get api for iOS
Now that we have a server and an interface, how can the client request and obtain data? Let's take a look at the simplest GET request example. Here we use the author's open-source library HYBNetworking:
NSString *url = @"http://www.api.com/index.php";[HYBNetworkinggetWithUrl:urlrefreshCache:YESsuccess:^(id response) { }fail:^(NSError *error) { }];
Let's take a look at the response results of the iOS client:
From the response results, we can see that the response is the same as that returned by our server interface, which shows the effect of browser access:
Php post Interface
Assume that we want to pass the type parameter, and the value is numeric, which is used to return different data. When we perform an interface test, we can do this, you don't need to wait for the backend to give the interface!
<? Php $ type = $ _ POST ['type']; $ data = ''; if (isset ($ type) & is_numeric ($ type) & amp; $ type & gt; = 0) {if ($ type = 1) {$ data = array ('type' => $ type, 'name' => 'Brother's technical blog ', 'SITE' => 'www .huangyibiao.com');} else if ($ type = 2) {$ data = array ('type' => $ type, 'name' => 'public account: Technical blog of ', 'SITE' => 'weixin search: biaogedejishuboke ');} $ response = array ('code' => 200, 'message' => 'Success for request', 'data' => $ data ,); echojson_encode ($ response); return ;}$ response = array ('code' => 999, 'message' => 'argument error for request ', 'data' => $ data,); echojson_encode ($ response );
IOS POST Interface
The following is how the iOS client calls the php post interface just written, and uses the author's open-source library HYBNetworking:
NSString *url = @"http://www.api.com/index.php";NSDictionary *params = @{@"type" : @(1)};[HYBNetworkingpostWithUrl:urlrefreshCache:YESparams:paramssuccess:^(id response) { }fail:^(NSError *error) { }];
Let's take a look at the following results. We can see that the interface data returned by the server is received and parsed as expected:
Summary
The content of this article has basically ended. If you want to learn it, you will definitely perform it carefully! In the future, we will gradually get into touch with it and learn about it, which will surely make your work more smooth! I hope this article will help you.