zoukankan      html  css  js  c++  java
  • php调用接口的应用小实例

     1 //GET方式的直接使用
     2 /*$file_contents = file_get_contents('http://localhost/Json/API.php?a=get_users&uid=10001&type=json');
     3 echo $file_contents;*/
     4 
     5 
     6 //POST方式得用下面的(需要开启PHP curl支持)。
     7 $url = 'http://localhost/Json/API.php?a=get_users&uid=10001&type=json';
     8 $ch = curl_init ();
     9 curl_setopt ( $ch, CURLOPT_URL, $url );
    10 curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    11 curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
    12 curl_setopt ( $ch, CURLOPT_POST, 1 ); //启用POST提交
    13 $file_contents = curl_exec ( $ch );
    14 echo $file_contents;
    15 curl_close ( $ch );
      1 <?php
      2 /**
      3  * Created by PhpStorm.
      4  * User: chensc
      5  * Date: 2017/5/24
      6  * Time: 15:01
      7  */
      8 header('Content-Type:text/html;charset=utf-8'); //确定编译码
      9 
     10 $output = array(); //定义数组output
     11 
     12 $a = @$_GET['a'] ? $_GET['a'] : '';//判断$a的值然后返回它
     13 //echo $a."<br/>";
     14 
     15 $uid = @$_GET['uid'] ? $_GET['uid'] : 0;//判断$的值然后返回它
     16 //echo $uid."<br/>";
     17 
     18 if (empty($a))
     19 {
     20     $output = array('data'=>NULL, 'info'=>'keng die', 'code'=>-201);
     21     exit(json_encode($output));
     22 }
     23 
     24 //走接口
     25 if ($a =='get_users')
     26 {
     27     //检查用户
     28     if ($uid == 0)
     29     {
     30         $output = array('data'=>NULL, 'info'=>'The uid is null!', 'code'=>-401);
     31         exit(json_encode($output));
     32     }
     33     //假设 $mysql 是数据库
     34     $mysql = array(
     35         10001 => array(
     36             'uid'=>10001,
     37             'vip'=>5,
     38             'nickname' => 'Shine X',
     39             'email'=>'979137@qq.com',
     40             'qq'=>979137,
     41             'gold'=>1500,
     42             'powerplay'=> array('2xp'=>12,'gem'=>12,'bingo'=>5,'keys'=>5,'chest'=>8),
     43             'gems'=> array('red'=>13,'green'=>3,'blue'=>8,'yellow'=>17),
     44             'ctime'=>1376523234,
     45             'lastLogin'=>1377123144,
     46             'level'=>19,
     47             'exp'=>16758,
     48         ),
     49         10002 => array(
     50             'uid'=>10002,
     51             'vip'=>50,
     52             'nickname' => 'elva',
     53             'email'=>'elva@ezhi.net',
     54             'qq'=>NULL,
     55             'gold'=>14320,
     56             'powerplay'=> array('2xp'=>1,'gem'=>120,'bingo'=>51,'keys'=>5,'chest'=>8),
     57             'gems'=> array('red'=>13,'green'=>3,'blue'=>8,'yellow'=>17),
     58             'ctime'=>1376523234,
     59             'lastLogin'=>1377123144,
     60             'level'=>112,
     61             'exp'=>167588,
     62         ),
     63         10003 => array(
     64             'uid' => 10003,
     65             'vip' => 5,
     66             'nickname' => 'Lily',
     67             'email' => 'Lily@ezhi.net',
     68             'qq' => NULL,
     69             'gold' => 1541,
     70             'powerplay'=> array('2xp'=>2,'gem'=>112,'bingo'=>4,'keys'=>7,'chest'=>8),
     71             'gems' => array('red'=>13,'green'=>3,'blue'=>9,'yellow'=>7),
     72             'ctime' => 1376523234,
     73             'lastLogin'=> 1377123144,
     74             'level' => 10,
     75             'exp' => 1758,
     76         ),
     77     );
     78 
     79     $uidArr = array(10001,10002,10003);
     80     if (in_array($uid, $uidArr, true))
     81     {
     82         $output = array('data' => NULL, 'info'=>'The user does not exist!', 'code' => -402);
     83         exit(json_encode($output));
     84     }
     85     //查询数据库
     86     $userInfo = $mysql[$uid];
     87 
     88     //输出数据
     89     $output = array(
     90         'data' => array(
     91             'userInfo' => $userInfo,
     92             'isLogin' => true,//是否首次登陆
     93             'unread' => 4,//未读消息数量
     94             'untask' => 3,//未完成任务
     95         ),
     96         'info' => 'Here is the message which, commonly used in popup window', //消息提示,客户端常会用此作为给弹窗信息。
     97         'code' => 200, //成功与失败的代码,一般都是正数或者负数
     98     );
     99     exit(json_encode($output));
    100 }
    101 elseif ($a == 'get_games_result')
    102 {
    103     //...
    104     die('您正在调 get_games_result 接口!');
    105 }
    106 elseif ($a == 'upload_avatars')
    107 {
    108     //....
    109     die('您正在调 upload_avatars 接口!');
    110 }
    努力学习
  • 相关阅读:
    浅谈桶排思想及[USACO08DEC]Patting Heads 题解
    【知识总结】CSS中样式覆盖优先顺序
    【知识总结】Activiti工作流学习入门
    mac下安装nginx问题解决
    spring项目中dubbo相关的配置文件出现红叉的问题
    mybatis对java自定义注解的使用——入门篇
    自学spring过程中碰到的问题list,一个一个解决
    mac下常用软件整理
    小米面试题目(测试开发岗位——不是测试,是测试工具和平台的开发岗位)
    Knockoutjs:Component and Custom Elements(翻译文章)
  • 原文地址:https://www.cnblogs.com/snmic/p/6902512.html
Copyright © 2011-2022 走看看