A brief introduction to _php example of mobile APP development (API interface development) using PHP

Source: Internet
Author: User

First, a simple answer to two questions:

1, PHP can develop the client?

A: Yes, because PHP is a scripting language, is responsible for the completion of B/s architecture or C/s structure of the parts, namely: mainly for the development of the service side. However, PHP is not only on the Internet site, a PHP for Android (PFA) site that they will be able to release the programming model, tool box documents to enable PHP applications on Android. The main sponsor of the project is open source, IRONTEC,PFA, which uses scripting Layer for Android (sl4a), Androd scripting Environment (ASE), to achieve this, You can refer to their website for more technical insider information.

If you are interested you can refer to some relevant technical documents, such as: http://so.jb51.net/cse/search?q=php+for+android&s=10520733385329581432

2, why the choice of PHP as the development of the server's first choice?

A: Cross-platform (can run on Unix, LINUX, WINDOWS, Mac OS), low consumption (PHP consumes quite a few system resources), efficient (relative), MySQL's perfect partner, itself is free open source, ...

Second, how to use the PHP development API (Application Programming Interface, application programming interface)?

People who have done APIs should understand that the development API is simpler than developing the WEB, but the logic may be more complex, because the API is actually data output, without rendering the page, so there is no MVC (API only M and C),

1, as with WEB development, first need some relevant parameters, these parameters, will be sent by the client, perhaps get is POST, which requires the development team to agree with each other, or to develop a unified specification.

2, with the parameters, according to the application requirements, the completion of data processing, such as: task progress updates, app purchase, a game end data submission, etc.

3, after the data logic processing, return to the client needs to use the relevant data, such as: task status, internal purchase results, player information and so on

How does the data return to the client?

The form of direct output, such as JSON, XML, TEXT, and so on.

4, client access to the data you return, local and user interaction in the client

A simple API example that is temporarily written:

<?php $output = Array (); $a = @$_get[' a ']?
  $_get[' A ']: '; $uid = @$_get[' uid ']?
  $_get[' uid ': 0;
   if (empty ($a)) {$output = array (' Data ' =>null, ' info ' => ' pit Dad! ', ' Code ' =>-201);
   Exit (Json_encode ($output)); //GO interface if ($a = = ' get_users ') {//Check user if ($uid = = 0) {$output = array (' Data ' =>null, ' info ' => ' Th
      E uid is null! ', ' Code ' =>-401);
   Exit (Json_encode ($output)); //Suppose $mysql is the database $mysql = Array (10001 => array (' UID ' =>10001, ' VIP ' =>5, ' nickname ' =&gt ; ' Shine X ', ' email ' => ' 979137@qq.com ', ' QQ ' =>979137, ' Gold ' =>1500, ' Powerplay ' => array (' 2XP ' =>12, ' Gem ' =>12, ' Bingo ' =>5, ' Keys ' =>5, ' chest ' =>8 ', ' Gems ' => Array (' Red ' =>13, ' green ' =>3, ' Blue ' =>8, ' yellow ' =>17), ' CTime ' =>1376523234, ' lastlogin ' =>1377123144, ' level ' =>19, ' exp ' =>16758, ' 10002 => Array (' UID ' =>10002, ' VIP ' =>, ' nickname ' => ' Elva ', ' email ' => ' elva@ezhi.net ', ' QQ ' =>null, ' Gold ' =>14320, ' Powerplay ' => Array (' 2XP ' =>1, ' Gem ' =>120, ' Bingo ' =>51, ' Keys ' =>5, ' chest ' =>8), ' Gems ' => Array ('
      Red ' =>13, ' green ' =>3, ' Blue ' =>8, ' yellow ' =>17, ' CTime ' =>1376523234, ' Lastlogin ' =>1377123144, ' Level ' =>112, ' exp ' =>167588,), 10003 => Array (' UID ' => 10003, ' VIP ' =>
      5, ' nickname ' => ' Lily ', ' email ' => ' Lily@ezhi.net ', ' QQ ' => NULL, ' Gold ' => 1541, ' Powerplay ' => array (' 2XP ' =>2, ' Gem ' =>112, ' Bingo ' =>4, ' Keys ' =>7, ' chest ' =>8), ' Gems ' => Array (' r
      Ed ' =>13, ' green ' =>3, ' Blue ' =>9, ' yellow ' =>7, ' CTime ' => 1376523234, ' Lastlogin ' => 1377123144,
       ' Level ' =>, ' exp ' => 1758,);
       $UIDARR = Array (10001,10002,10003); if (In_array ($uid, $UIDARR, True) {$output = array (' Data ' => NULL, ' info ' => ' The user does not exist! ', ' Code ' =>-402);
       Exit (Json_encode ($output));
       //Query Database $userInfo = $mysql [$uid]; Output Data $output = Array (' Data ' => array (' UserInfo ' => $userInfo, ' IsLogin ' => tru e,//whether the first login ' unread ' => 4,//unread message number ' Untask ' => 3,//unfinished Task), ' info ' => ' is the
      Message which, commonly used in popup window ',//Messages prompt, the client often uses this as the window information.
       ' Code ' => 200,//success and failure codes, usually positive or negative numbers;
    Exit (Json_encode ($output)); 
      ElseIf ($a = = ' Get_games_result ') {//... die (' You are tuning Get_games_result interface! ')
    
      ElseIf ($a = = ' Upload_avatars ') {//... die (' You are tuning Upload_avatars interface! ')
 }

Click Test (for the client, it is also a direct call to this address):

http://www.ezhi.net/api/test/index.php

Http://www.ezhi.net/api/test/index.php?a=get_users

http://www.ezhi.net/api/test/index.php?a=get_users&uid=10001

http://www.ezhi.net/api/test/index.php?a=get_users&uid=10002

http://www.ezhi.net/api/test/index.php?a=get_users&uid=10003

Three, in the actual project, we should notice several matters in the development API (for reference only):

1, a single file to achieve multiple interfaces in the form of many kinds, such as: if ... ElseIf.. or switch or dynamic method (that is, the form of this access function body of TP)

2, for the output of data best with Json,json has a very strong cross-platform, the major mainstream programming languages on the market support JSON parsing, JSON is gradually replacing XML, as a common format for network data

3, interface security, we must increase the interface validation. For example, the client and the server are unified encryption for different interfaces, and the server needs to be validated for each interface. To ensure that the interface is prevented from being maliciously refreshed or maliciously invoked by hackers, especially in large commercial applications.

4, for the online API must ensure that all interfaces are normal and turn off all error messages => error_reporting (0), in the output JSON, there can be no other output, otherwise, the client will get the wrong data information, 98% directly lead to the client crash!

5, the development of the API and the web has a certain difference, if it is the Web, the code may be wrong, will not cause particularly serious errors, perhaps just caused the data write and query failed, may lead to a certain part of the web dislocation or garbled. But if is the api,99% situation all is the client direct crash, the flash-back!

6, DO interface development, do not recommend the use of framework development, the reasons for two points (in fact, I am a bit risky, I am also a tper, after all, this is TP's official website):

The client generally to the service side response speed has the extremely high request, therefore, uses the most original ecology PHP completes the interface development, is most efficient, if uses the frame, also needs to load each kind does not need the superfluous document, like Summer wears the winter clothes. Just imagine, when you play mobile phone, use an application of a random operation, and so on a half-day to have movement, you suffer?
The 4th mentioned above, the framework for Web development is a very happy thing, but for the API, you can not imagine it will give you something wrong! In the end you'll be miserable because many frameworks are created for the WEB (I'm also looking forward to seeing one day a framework or extension specifically for the development API)
Speaking of which, I have to say that the open platform that has swept the Internet. In fact those open platforms, the so-called open, is to provide you with an interface that you can access based on the technical documentation they provide, the format and requirements they provide, and the interface files they offer (typically returns JSON or XML), and you get information about them, such as: QQ user profile, Taobao store, commodity news and so on. Then on the basis of these messages, the interaction is completed in your application.

In fact, Ajax is also an embodiment of the calling API, do you think? Oh ~ ~

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.