How to use PHP to write app interface _php example

Source: Internet
Author: User
Tags configuration php

Build PHP Environment

Because I use the computer is a Mac, so recommend that you use Mamp Pro this software, but the fee version, I believe you do not want to spend money will have a way to fix!

Mamp Pro This software is integrated environmental software, has Apache, MySQL, PHP, with the operation of the environment to parse PHP!

If you don't want to use an integrated environment, you can build it yourself. Mac with Apache and PHP environment, only need a little configuration can be used, and then install the MySQL on it!

You can look at the simple version configuration: Mac configuration PHP Environment this article.

Start the server

Here's how to start the server with Mamp Pro software as an example. As shown below, we assume that the service address is www.api.com and then associate the project directory with the service address, looking at the lower-right corner of the graph:

After the environment is started, you can enter the www.api.com directly in the browser to run!

Start Get interface

After the first run of our empty project, we will automatically generate index.php this file, we delete the contents, and then modify the following:

 <?php
 
$data = Array (
 ' tid ' =>, ' 
 name ' => ' Technical blog ', '
 site ' => ' www.huangyibiao.com '); c5/> $response = Array (
 ' code ' =>, 
 ' message ' => ' success for request ',
 ' data ' => $data,
 ); C10/>echojson_encode ($response);

Is our structure here the most common return data structure in the daily app development? They are usually status codes, state information, and client business data.

PHP is also good to force, declare the array, through the json_encode function can output JSON format data!

iOS tune Get interface

We have the server and the interface, so how can the client request and how to get the data? Let's take a look at the simplest GET request example, which uses the Hybnetworking author's Open Source Library:

NSString *url = @ "http://www.api.com/index.php";
[hybnetworkinggetwithurl:urlrefreshcache:yessuccess:^ (ID response) {
 
}fail:^ (nserror *error) {
 
}];

Let's take a look at the results of the iOS client response:

As you can see from the response results, it is consistent with what our server interface returns, looking at the effect of the following image in the browser:

PHP Post Interface

Suppose we want to pass the parameter type over, and the requirement is numeric, used to return different data, when we do interface test, we can do so, do not need to wait for the background to the interface!

<?php
 
$type = $_post[' type '];
$data = ';
 
if (Isset ($type) && is_numeric ($type) && $type >= 0) {
 if ($type = = 1) {
 $data = array (
  ' Ty PE ' => $type, 
  ' name ' => ' Technical blog ', '
  site ' => ' www.huangyibiao.com ');
 } else if ($type = = 2) {
 $data = Array (
  ' type ' => $type, 
  ' name ' => ' public number: Standard Brother's Technical blog ',
  ' site ' => ' Weixin search: Biaogedejishuboke ');
 } 
 
 $response = Array (
 ' code ' =>, 
 ' message ' => ' success for request ',
 ' data ' => $data,
 ); C22/>echojson_encode ($response);
 return;
} 
 
$response = Array (
 ' code ' => 999, ' message 
 ' => ' argument error for request ',
 ' data ' => $data, 
   );
 
Echojson_encode ($response);

iOS Tuning Post interface

Here's how the iOS client invokes the PHP post interface just written, which uses the Hybnetworking author's Open Source Library:

NSString *url = @ "http://www.api.com/index.php";
Nsdictionary *params = @{@ "type": @ (1)};
[hybnetworkingpostwithurl:urlrefreshcache:yesparams:paramssuccess:^ (ID response) {
 
}fail:^ (nserror *error) {
 
}];

We look at the results as follows, you can see that the server returned to receive the interface data and parse it out:

Summarize

The content of this article to this is basically over, I believe that if you want to learn it, we will seriously to operate it again! In the future will slowly contact with it, slowly grasp it, will make you work more smoothly! I hope this article can be helpful to everyone.

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.