A brief discussion on PHP and mobile APP development (API interface development)

WBOY
Release: 2016-07-29 09:13:44
Original
908 people have browsed it
Recommended reading:RESTful What is it? Let’s understand RESTful architecture together Get a deeper understanding of API development
This post is written for people who don’t know much about PHP and API development
1. First, briefly answer two questions:
1. Can PHP develop clients?
Answer: No, because PHP is a scripting language and is responsible for completing the S part of the B/S architecture or C/S architecture, that is, the development of the server. (Don’t worry about GTK and WinBinder)
2. Why choose PHP as your first choice for server development?
Answer: Cross-platform (can run under UNIX, LINUX, WINDOWS, Mac OS), low consumption (PHP consumes quite a few system resources), high operating efficiency (relatively speaking), the perfect partner of MySQL, itself Free and open source,...
2. How to use PHP to develop API (ApplicationProgramming Interface, Application Programming Interface)?
People who have done API should understand that actually developing API is simpler than developing WEB, but the logic may be more complicated, because API is actually data output without rendering the page, so there is no MVC (API only has M and C) ,
1. Just like WEB development, you first need some relevant parameters. These parameters will be passed by the client, maybe GET or POST. This needs to be agreed upon by the development team or a unified specification can be formulated.
2. With parameters, complete data processing according to application requirements, such as: task progress update, APP in-app purchase, data submission at the end of a game, etc.
3. After the data logic is processed, return to the client. Relevant data that needs to be used, such as: mission status, in-app purchase results, player information, etc. How to return the data to the client?
Direct output form, such as: JSON, XML, TEXT, etc.
4. After the client gets the data you returned, it interacts with the user locally on the client
A simple API example written temporarily:
  1. php
  2. $output=array();
  3. $a=@$_GET['a']?$_GET['a']:'';
  4. $uid=@$_GET['uid']?$_GET['uid']:0;
  5. if(empty($a)){
  6. gt;NULL,'info'=>exit(json_encode($output));}
  7. // Take the interface
  8. if($a==
  9. 'get_users')
  10. {
  11. //Check users
  12. if($uid==$output ;NULL,'info'
  13. =>'The uid is null!',
  14. 'code'=>-401);exit(json_encode($output));
  15. }
  16. //Assume $mysql is the database
  17. $mysql=array(
  18. 10001=>array(
  19. =>5,
  20. =>'Shine X',
  21. .com'
  22. ,' qq' = & gt;979137,
  23. 'gold'= & gt;1500,
  24. 'powerPlay'= & gt;array array('2xp'
  25. =>
  26. 12,'gem'=> ;5
  27. ,
  28. 'keys'=>5,'chest'=>'gems'=>array('red'=>13,'green'=>8,'yellow'=>17),4,
  29. =>1377123144,'exp'=>16758,),10002=>'elva',
  30. email'=>'elva@ezhi.net',
  31. => >1,'gem'=>120
  32. ,'bingo'=>'chest'=>8
  33. ),
  34. 'gems'=>array('red'=>13,'green'=>3,'blue'=>8,'yellow'=>17),
  35. 'ctime'=>1376523234,
  36. 'lastLogin'=>1377123144,
  37. 'level'=>112,
  38. 'exp'=>167588,
  39. ),
  40. 10003=>array(
  41. 'uid'=>10003,
  42. 'vip'=>5,
  43. 'nickname'=>'Lily',
  44. 'email'=>'Lily@ezhi.net',
  45. 'qq'=>NULL,
  46. 'gold'=>1541,
  47. 'powerplay'=>array('2xp'=>2,'gem'=>112,'bingo'=>4,'keys'=>7,'chest'=>8),
  48. 'gems'=>array('red'=>13,'green'=>3,'blue'=>9,'yellow'=>7),
  49. 'ctime'=>1376523234,
  50. 'lastLogin'=>1377123144,
  51. 'level'=>10,
  52. 'exp'=>1758,
  53. ),
  54. );
  55. $uidArr=array(10001,10002,10003);
  56. if(in_array($uid,$uidArr,true)){
  57. $output=array('data'=>NULL,'info'=>'The user does not exist!','code'=>-402);
  58. (json_encode($output));
  59. }
  60. [$uid
  61. ];
  62. //Output data
  63. $output
  64. =
  65. array
  66. (
  67. )
  68. 'data'=>array
  69. (
  70. =>true,
  71. //Is it the first time Log in u'unream'= & gt;4,// Number of unusual messages
  72. 'untask'= & gt;3,//Unfinished tasks
  73. ),=>'Here is the message which , commonly used in popup window'
  74. ,
  75. //Message prompt, the client often uses this as a pop-up window message.200,//The codes for success and failure are usually positive or negative numbersson_encode
  76. ($output);
  77. {
  78. //. ..die('You are adjusting the get_games_result interface!');
  79. }elseif($a=='upload_avatars')
  80. {
  81. upload_avatars interface!'
  82. );}Copy codeClick test (for the client, this address is also called 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 actual projects, we should pay attention to several matters when developing API ( For reference only):
    1. There are many ways to implement multiple interfaces in a single file, such as: if..elseif.. or switch or dynamic method (that is, TP’s form of accessing the function body)
    2. It is best to use json for data output. JSON is very cross-platform. All major mainstream programming languages in the market support json parsing. JSON is gradually replacing xml and becoming the universal format for network data
    3. Interface For security, interface verification must be added. For example, the client and server use unified encryption methods for different interfaces, and the server needs to verify each interface. This is to ensure that the interface is prevented from being maliciously refreshed or maliciously called by hackers, especially for large commercial applications.
    4. For online APIs, you must ensure that all interfaces are normal and all error messages are closed => error_reporting(0). When outputting JSON, there cannot be any other output. Otherwise, the client will fail to parse the data and directly Crash!
    5. There is a certain difference between developing API and WEB. If it is WEB, the code may be wrong, which will not cause a particularly serious error. It may just cause data writing and query failure, or it may cause a certain part of the WEB to be misaligned. Or gibberish. But if it’s an API, just Crash!
    6. When doing interface development, it is not recommended to use framework development. The reasons can be summarized as follows (in fact, I am a bit risky, I am also a TPer, after all, this is the official website of TP):
      1) Client Generally, there are extremely high requirements for the response speed of the server. Therefore, it is most efficient to use the most original PHP to complete interface development. If a framework is used, various unnecessary files need to be loaded, just like wearing a suit in the summer. winter clothes. Just imagine, when you are playing on your mobile phone, you use an application to perform any operation, and wait for a long time before there is any movement. Can you bear it?
      2) As mentioned in point 4 above, frameworks are a very happy thing for WEB development, but for APIs, you really can’t imagine what trouble it will cause you! In the end, you will be miserable~~ Because many frameworks were born for the WEB (I also look forward to one day seeing frameworks or extensions specifically designed for developing APIs)
     Some people are also struggling with this, interface efficiency and stability , it also depends on the person who codes. Some people may not be able to write as fast as the framework, while others think there is no problem with using the framework. These are just suggestions. The key depends on your actual situation. It is also recommended to stress test the code before going online
    Speaking of which, I have to talk about open platforms such as Tencent Weibo and Taobao. In fact, those open platforms, so-called open, provide you with such an interface. You can adjust the interface files they provide (generally returning JSON or XML) based on the technical documents they provide and the formats and requirements they set. You can get their relevant information, such as: QQ user basic information,Taobao store, product news, etc. Then complete the interaction in your application based on these messages.
    In fact,ajaxis also a form of calling API, what do you think? Haha~~

    The above has introduced a brief discussion of PHP and mobile APP development (API interface development), including ajax, Taobao store, and Application content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!