Table of Contents
浅谈使用 PHP 进行手机 APP 开发(API 接口开发),appapi
使用php开发app的接口,什框架比较好?
怎使用PHP开发API?
Home php教程 php手册 浅谈使用 PHP 进行手机 APP 开发(API 接口开发),appapi

浅谈使用 PHP 进行手机 APP 开发(API 接口开发),appapi

Jun 13, 2016 am 09:27 AM
api app php Interface development

浅谈使用 PHP 进行手机 APP 开发(API 接口开发),appapi

一、先简单回答两个问题:

1、PHP 可以开发客户端?

答:可以,因为PHP是脚本语言,是负责完成 B/S架构 或 C/S架构 的S部分,即:主要用于服务端的开发。但是,PHP可不仅仅只能在互联网站上发展,一个PHP for Android(PFA)站点表示他们将可以发布编程模型、工具盒文档让PHP在Android上实现应用。该项目的主要赞助商是开源公司IronTec,PFA使用Scripting Layer for Android (SL4A),也就是Androd Scripting Environment(ASE)来实现这一点,您可以参看他们的网站来了解更多技术内幕。

如果有兴趣你可以参考一些相关的技术文档,比如:http://so.jb51.net/cse/search?q=php+for+android&s=10520733385329581432

2、为什么选择 PHP 作为开发服务端的首选?

答:跨平台(可以运行在UNIX、LINUX、WINDOWS、Mac OS下)、低消耗(PHP消耗相当少的系统资源)、运行效率高(相对而言)、MySQL的完美搭档,本身是免费开源的,......

二、如何使用 PHP 开发 API(Application Programming Interface,应用程序编程接口) 呢?

做过 API 的人应该了解,其实开发 API 比开发 WEB 更简洁,但可能逻辑更复杂,因为 API 其实就是数据输出,不用呈现页面,所以也就不存在 MVC(API 只有 M 和 C),

1、和 WEB 开发一样,首先需要一些相关的参数,这些参数,都会由客户端传过来,也许是 GET 也许是 POST,这个需要开发团队相互之间约定好,或者制定统一规范。

2、有了参数,根据应用需求,完成数据处理,例如:任务进度更新、APP内购、一局游戏结束数据提交等等

3、数据逻辑处理完之后,返回客户端所需要用到的相关数据,例如:任务状态、内购结果、玩家信息等等

数据怎么返给客户端?

直接输出的形式,如:JSON、XML、TEXT 等等。

4、客户端获取到你返回的数据后,在客户端本地和用户进行交互

临时写的一个简单 API 例子:

<&#63;php
  $output = array();
  $a = @$_GET['a'] &#63; $_GET['a'] : '';
  $uid = @$_GET['uid'] &#63; $_GET['uid'] : 0;
  if (empty($a)) {
   $output = array('data'=>NULL, 'info'=>'坑爹啊!', 'code'=>-201);
   exit(json_encode($output));
   }
   
 //走接口
  if ($a == 'get_users') {
   //检查用户
   if ($uid == 0) {
     $output = array('data'=>NULL, 'info'=>'The uid is null!', 'code'=>-401);
      exit(json_encode($output));
   }
   //假设 $mysql 是数据库
  $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));
       }
   
       //查询数据库
       $userInfo = $mysql[$uid];
       //输出数据
       $output = array(
        'data' => array(
        'userInfo' => $userInfo,
        'isLogin' => true,//是否首次登陆
        'unread' => 4,//未读消息数量
        'untask' => 3,//未完成任务
        ), 
      'info' => 'Here is the message which, commonly used in popup window', //消息提示,客户端常会用此作为给弹窗信息。
      'code' => 200, //成功与失败的代码,一般都是正数或者负数
        );
       exit(json_encode($output));
    } elseif ($a == 'get_games_result') {
    
       //... 
       die('您正在调 get_games_result 接口!'); 
      } elseif ($a == 'upload_avatars') { 
       //.... 
       die('您正在调 upload_avatars 接口!');
    
      }
Copy after login

点击测试(对于客户端而言,也是直接调用这样的地址):

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

三、实际项目中,我们在开发 API 应该注意的几个事项(仅供参考):

1、单文件实现多接口的形式有很多种,例如:if..elseif.. 或 switch 或 动态方法 (也就是TP的这种访问函数体的形式)

2、对于数据的输出最好用json,json具有相当强大的跨平台性,市场上各大主流编程语言都支持json解析,json正在逐步取代xml,成为网络数据的通用格式

3、接口安全,一定要增加接口验证。例如,客户端和服务端针对不同接口统一做好加密方式,服务端在对于每次接口需要都要进行验证。以保证防止接口被恶意刷新或黑客恶意调用,尤其是大型商业应用。

4、对于线上的 API 必须保证所有接口正常且关闭所有的错误信息 => error_reporting(0),在输出JSON 时,不能有任何其它输出,否则,客户端将会获取错误的数据信息,98%直接导致客户端 Crash!

5、开发 API 和 WEB 有一定的区别,如果是 WEB 的话,可能代码出错了,不会导致特别严重的错误,也许只是导致数据写入和查询失败,也许导致 WEB 的某个部分错位或乱码。但如果是 API,99%的情况都是客户端直接Crash、闪退!

6、做接口开发,不建议使用框架开发,原因概括起来有两点(其实我有点冒风险的,本人也是 TPer 一枚,毕竟这是TP的官网):

客户端一般对服务端的响应速度有极高要求,因此,使用最原生态的 PHP完成接口开发,是最高效的,假如用到了框架,还需要加载各种不需要多余的文件,就好比夏天穿了件冬天的衣服。试想,你在玩手机的时候,使用一个应用随便一个操作,等半天才有动静,你受的了吗?
就是上面第4点提到的,框架对于WEB开发,是件很幸福的事,但对于 API 而言,你实在不敢想象它会给你出什么岔子!最后你将痛苦不堪~~因为很多框架都是为 WEB 诞生的(我也很期待有一天能看到专门为开发 API 而生的框架或者扩展)
说到这,不得不说扯一下,风靡互联网的开放平台。其实那些开放平台,所谓的开放,就是给你提供一个这样的接口,你根据他们提供的技术文档,按他们制定的格式和要求,调它们提供的接口文件(一般都是返回JSON或者XML),你就可以获取到他们的相关信息,例如:QQ用户基本信息、淘宝店铺、商品消息等等。然后在根据这些消息,在你的应用里完成交互。

其实,ajax 也是调用 API 的一种体现形式,你觉得呢? 呵呵~~

使用php开发app的接口,什框架比较好?

开发app和架构没关系,选择架构要看功能上的需求,thinkphp相对来说功能比较复杂,当然提供的模块也很多,如果开发轻型app 试试ci 吧,一个是容易上手,而且体积小,功能也很完善!
 

怎使用PHP开发API?

1. SOAP
2. XML-RPC
 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles