码迷,mamicode.com
首页 > Web开发 > 详细

php调用接口的应用小实例

时间:2017-05-25 11:47:26      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:成功   htm   ora   evel   init   post   gem   header   nsf   

 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 }

 

php调用接口的应用小实例

标签:成功   htm   ora   evel   init   post   gem   header   nsf   

原文地址:http://www.cnblogs.com/snmic/p/6902512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!