People who have worked on APIs should understand that developing APIS is more concise than developing WEB, but the logic may be more complex, because APIs are actually data output and do not need to present pages, so there is no MVC (the API only has M and C
People who have worked on APIs should understand that developing APIS is more concise than developing WEB, but the logic may be more complex, because APIs are actually data output and do not need to present pages, so there is no MVC (the API only has M and C
I. answer two questions:
1. Can PHP develop clients?
A: Yes. Because PHP is a scripting language, it is responsible for completing the B/S architecture or the S part of the C/S architecture, that is, it is mainly used for server development. However, PHP can only be developed on the Internet. a php for Android (PFA) site indicates that they will be able to publish programming models and Toolbox documents for PHP to implement applications on Android. The main sponsor of this project is IronTec, an open-source company. PFA uses Scripting Layer for Android (SL4A), that is, Androd Scripting Environment (ASE) to achieve this, you can refer to their website to learn more about the technology.
If you are interested, you can refer to some related technical documents, such:
2. Why is PHP preferred for the development server?
A: cross-platform (running on UNIX, LINUX, WINDOWS, and Mac OS), low consumption (PHP consumes a considerable amount of system resources), and high running efficiency (relatively speaking) the perfect partner of MySQL is free and open-source ,......
2. How to Use PHP to develop an API (Application Programming Interface?
People who have worked on APIs should understand that developing APIS is more concise than developing WEB, but the logic may be more complex, because APIs are actually data output and do not need to present pages, so there is no MVC (the API only has M and C ),
1. Like WEB development, some related parameters are required first. These parameters will be transmitted from the client, maybe GET or POST. This requires mutual agreement between the development teams, or a unified specification can be formulated.
2. Complete data processing based on application requirements with parameters, such as task progress update, in-APP purchase, and data submission for the end of a game.
3. After the data logic is processed, return the relevant data required by the client, such as task status, internal purchase result, and player information.
How to return data to the client?
The form of direct output, such as JSON, XML, and TEXT.
4. After the client obtains the data you returned, it interacts with the user locally on the client.
A Simple API example for temporary writing:
<? Php $ output = array (); $ a = $ _ GET ['a']? $ _ GET ['a']: ''; $ uid = @ $ _ GET ['uid']? $ _ GET ['uid']: 0; if (empty ($ a) {$ output = array ('data' => NULL, 'info' => 'Ah! ', 'Code' =>-201); exit (json_encode ($ output);} // go to the interface if ($ a = 'get _ users ') {// check if ($ uid = 0) {$ output = array ('data' => NULL, 'info' => 'the uid is null! ', 'Code' =>-401); exit (json_encode ($ output ));} // assume that $ mysql is a database $ mysql = array (10001 => array ('uid' => 10001, 'vip '=> 5, 'nickname' => 'shine x', 'email '=> '2017 @ qq.com', 'qq' => 979137, 'gold' => 979137, 'powerplay' => array ('2xp '=> 12, 'gems' => 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, 'G' => 120, 'stringo' => 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, 'G' => 112, 'bingo' => 4, 'keys' => 7, 'chest '=> 8), 'gems' => array ('red' => 13, 'green' => 3, 'Blue '=> 9, 'yellow' => 7), 'ctime' => 1376523234, 'lastlogin' => 1377123144, 'level' => 10, 'expires' => 1758,),); $ uidArr = array (10003,); if (in_array ($ uid, $ uidArr, true )) {$ output = array ('data' => NULL, 'info' => 'user does not exist! ', 'Code' =>-402); exit (json_encode ($ output);} // query the database $ userInfo = $ mysql [$ uid]; // output data $ output = array ('data' => array ('userinfo' => $ userInfo, 'islogin' => true, // whether to log on to 'unread' => 4 for the first time, // The number of unread messages: 'untask' => 3, // The task is not completed ), 'info' => 'here is the message which, commonly used in popup Window', // message prompt, which is often used by the client as a pop-up window information. 'Code' => 200, // code for success and failure, usually positive or negative); exit (json_encode ($ output ));} elseif ($ a = 'get _ games_result '){//... die ('you are calling the get_games_result interface! ');} Elseif ($ a = 'upload _ avatars') {//... die ('you are calling the upload_avatars interface! ');}
Click test (this address is also called directly for the client ):
3. In actual projects, we should pay attention to the following items during API development (for reference only ):
1. There are many forms of multi-interface implementation in a single file, such as: if... elseif... or switch or dynamic method (that is, the form of accessing the function body like TP)
2. It is best to use json for data output. json is quite powerful across platforms. Major programming languages on the market support json parsing. json is gradually replacing xml, become a common format of Network Data
3. API verification must be added for interface security. For example, if the client and server use a unified encryption method for different interfaces, the server must verify each interface. To prevent the interface from being maliciously refreshed or maliciously called by hackers, especially for large commercial applications.
4. For online APIs, ensure that all interfaces are normal and disable all error messages => error_reporting (0). No other output is allowed when JSON is output. Otherwise, the client will get the wrong data information, 98% directly causes the client Crash!
5. There are some differences between API development and WEB development. If it is WEB, code errors may occur without causing serious errors. It may only cause Data Writing and query failures, this may cause a part of the WEB to be misplaced or garbled. But for APIS, 99% of the cases are client-side direct Crash and Crash!
6. For interface development, it is not recommended to use framework development. There are two reasons in summary (in fact, I am a bit risky, I am also a TPer, after all, this is the TP official website ):