PHP programming simple App interface, php programming app

Source: Internet
Author: User
Tags configuration php php development environment

PHP programming simple App interface, php programming app

This is the first PHP interface to be written in the notes, and is used for application testing in iOS development. Today, we will share with you how to write your own interface for testing!

I believe many of my friends have encountered this problem during development: When will the backend provide interfaces? How can I provide an interface when other interfaces are provided? How can we achieve this without interfaces?

Haha! After completing this article, you can create an interface to return fixed dead data for testing!

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!

You can take a look at the simple version configuration: The new Mac configuration PHP development environment tutorial this article.

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 iswww.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 enterwww.api.comRunning!

Start GET Interface

Our empty project will be automatically generated after the first runindex.phpIn this file, 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_encodeThe 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

This article ends here. I believe that if you want to learn it, you will do it carefully! Before writing this article, I have tried it!

In the future, we will gradually get into touch with it and learn about it, which will surely make your work more smooth!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.