PHP simple to write app interface program example

Source: Internet
Author: User

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!

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:

The technical blog of Brother

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 ' => 100,
' Name ' => ' Technical blog ',
' Site ' => ' www.111cn.net ');

$response = Array (
' Code ' => 200,
' Message ' => ' success for Request ',
' Data ' => $data,
);

echo Json_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";
[Hybnetworking getwithurl:url refreshcache:yes success:^ (ID response) {

} fail:^ (Nserror *error) {

}];

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

The technical blog of Brother

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 (
' Type ' => $type,
' Name ' => ' Technical blog ',
' Site ' => ' www.111cn.net ');
else if ($type = = 2) {
$data = Array (
' Type ' => $type,
' Name ' => ' public: Standard Brother's Technical blog ',
' Site ' => ' Weixin search:biaogedejishuboke ');
}

$response = Array (
' Code ' => 200,
' Message ' => ' success for Request ',
' Data ' => $data,
);

echo Json_encode ($response);
Return
}

$response = Array (
' Code ' => 999,
' Message ' => ' argument the error for request ',
' Data ' => $data,
);

echo Json_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)};
[hybnetworking postwithurl:url refreshcache:yes params:params success:^ (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:

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.