php調用介面的應用小執行個體

來源:互聯網
上載者:User

標籤:成功   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‘=>‘[email protected]‘, 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‘=>‘[email protected]‘, 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‘ => ‘[email protected]‘, 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調用介面的應用小執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.