Talking about the development of mobile phone APP using PHP (API interface development)

  • 2021-07-10 19:07:17
  • OfStack

1. Answer two simple questions first:

1. Can PHP develop clients?

A: Yes, because PHP is a scripting language, which is responsible for completing the S part of B/S architecture or C/S architecture, that is, it is mainly used for server-side development. However, PHP can not only be developed on Internet sites, but an PHP for Android (PFA) site indicates that they will be able to publish programming models and toolbox documents for PHP to be applied on Android. The main sponsor of this project is the open source company IronTec, and PFA uses Scripting Layer for Android (SL4A), that is, Androd Scripting Environment (ASE) to achieve this point. You can refer to their website for more technical inside information.

If you are interested, you can refer to one related technical document, such as: http://so.ofstack.com/cse/search? q=php+for+android & s=10520733385329581432

2. Why choose PHP as the first choice for development server?

A: Cross-platform (can run under UNIX, LINUX, WINDOWS, Mac OS), low consumption (PHP consumes quite little system resources), high operation efficiency (relatively speaking), the perfect partner of MySQL, itself is free and open source,......

2. How to develop API (Application Programming Interface, application programming interface) using PHP?

Those who have done API should understand that developing API is actually simpler than developing WEB, but the logic may be more complicated, because API is actually data output without rendering pages, so there is no MVC (API only M and C).

1. To develop a sample with WEB, first of all, you need some related parameters. These parameters will be transmitted from the client, perhaps GET or POST. This requires the development team to agree with each other or formulate a unified 1 specification.

2. With parameters, complete data processing according to application requirements, such as task progress update, APP internal purchase, 1 game end data submission and so on

3. After the data logic is processed, return the relevant data needed by the client, such as: task status, in-house purchase results, player information and so on

How can the data be returned to the client?

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

4. After the client gets the data you return, it interacts with the user locally

A simple example of API written temporarily:


<?php
  $output = array();
  $a = @$_GET['a'] ? $_GET['a'] : '';
  $uid = @$_GET['uid'] ? $_GET['uid'] : 0;
  if (empty($a)) {
   $output = array('data'=>NULL, 'info'=>' Pity dad !', 'code'=>-201);
   exit(json_encode($output));
   }
   
 // Walk interface 
  if ($a == 'get_users') {
   // Check users 
   if ($uid == 0) {
     $output = array('data'=>NULL, 'info'=>'The uid is null!', 'code'=>-401);
      exit(json_encode($output));
   }
   // Hypothesis  $mysql  Is a database 
  $mysql = array(
   10001 => array(
     'uid'=>10001,
     'vip'=>5,
     'nickname' => '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'=>50,
      '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('red'=>13,'green'=>3,'blue'=>9,'yellow'=>7),
      'ctime' => 1376523234,
      'lastLogin'=> 1377123144,
      'level' => 10,
      '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' => true,// Is it the first time to log in 
        'unread' => 4,// Number of unread messages 
        'untask' => 3,// Unfinished task 
        ), 
      'info' => 'Here is the message which, commonly used in popup window', // Message prompt, which is often used by clients as pop-up information. 
      'code' => 200, // Success and failure code, 1 They are generally 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 also calls such an address directly):

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

3. In the actual project, we should pay attention to several things when developing API (for reference only):

1. There are many forms of implementing multiple interfaces in a single file, such as if... elseif... or switch or dynamic methods (that is, the form of this access function body in TP)

2. It is better to use json for data output. json has strong cross-platform property. json is supported by all major mainstream programming languages in the market. json is gradually replacing xml and becoming the general format of network data

3. Interface security. 1. Interface verification must be added. For example, the client and server are encrypted for different interfaces, and the server needs to verify each interface. So as to prevent the interface from being maliciously refreshed or maliciously called by hackers, especially for large commercial applications.

4. For on-line API, all interfaces must be ensured to be normal and all error messages must be turned off = > error_reporting (0), when outputting JSON, there can be no other output, otherwise, the client will get the wrong data information, 98% directly lead to the client Crash!

5. There is a definite difference between developing API and WEB. If it is WEB, the code may be wrong, which will not lead to particularly serious errors. It may only lead to data writing and query failure, and may lead to some part of WEB misplacement or garbled. But if it is API, 99% of the cases are Crash directly from the client, flashback!

6. For interface development, it is not recommended to use framework development. There are two reasons (in fact, I am a little risky, and I am also one TPer. After all, this is the official website of TP):

Client 1 generally has extremely high requirements for the response speed of server. Therefore, it is the most efficient to use the most original PHP to complete interface development. If the framework is used, it is necessary to load various unnecessary files, just like wearing a winter dress in summer. Imagine, when you are playing with your mobile phone, use one application to operate casually, and wait for half a day before there is any movement. Can you stand it?
As mentioned in point 4 above, the framework is a very happy thing for WEB development, but for API, you can't imagine what will go wrong with it! Eventually you'll be miserable ~ ~ because many of the frameworks were created for WEB. (I'm also looking forward to seeing a framework or extension specifically created for API someday.)
Speaking of which, I have to say that it is a popular open platform on the Internet. In fact, those open platforms, the so-called open, are to provide you with such an interface. According to the technical documents provided by them, according to the format and requirements formulated by them, adjust the interface files provided by them (1 is generally returned to JSON or XML), and you can get their relevant information, such as QQ user basic information, Taobao shop, commodity information and so on. Then, according to these messages, the interaction is completed in your application.

In fact, ajax is also a form of calling API, don't you think? Hehe ~ ~


Related articles: