Dzejkobini

mobileMuApi

Apr 12th, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.64 KB | None | 0 0
  1. <?php
  2.  
  3. $server = $_GET["server"];
  4. $login = $_GET["login"];
  5. $id = $_GET["id"];
  6.  
  7. // Dane logowania
  8. $loginParams = array (
  9.     "primera" => array("username" => "Veigar","password" => "veivei"),
  10.     "suna" => array("username" => "Veigar","password" => "veivei"),
  11.     "secura" => array("username" => "Veigar","password" => "veivei")
  12. );
  13.  
  14.  
  15. if(empty($server))
  16. {
  17.     print_r(json_encode(array("error" => "type server name")));
  18. }
  19. elseif(!array_key_exists($server, $loginParams))
  20. {
  21.     print_r(json_encode(array("error" => $server . " server is not supported")));
  22. }
  23. elseif(!empty($server))
  24. {
  25.     $accessToken = getAccessToken($loginParams, $server);
  26.     print_r(getUserInfo($id, $login, $server, $accessToken));
  27. }
  28.  
  29.  
  30.  
  31. // Pobieranie access tokena
  32. function getAccessToken($loginParams, $server)
  33. {
  34.     $jsonString = json_encode($loginParams[$server]);
  35.     $ch = curl_init();
  36.     curl_setopt($ch, CURLOPT_URL, "http://".$server.".e-sim.org/mobile/login");
  37.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  38.     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  39.     curl_setopt($ch, CURLOPT_POST, true);
  40.     curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonString);
  41.     curl_setopt($ch, CURLOPT_HEADER, true);
  42.     curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
  43.         'Content-Type: application/json',                                                                                
  44.         'Content-Length: ' . strlen($jsonString)                                                                      
  45.     ));      
  46.     $output = curl_exec($ch);
  47.     curl_close($ch);
  48.     //Pozycja pierwszego nawiasu - {
  49.     $pos = strpos($output, "{");
  50.     //JSON substring
  51.     $substr = substr($output, $pos);
  52.  
  53.     $jsonObj = json_decode($substr);
  54.     $access_token = $jsonObj->token;
  55.     return $access_token;
  56. }
  57.  
  58. // Pobieranie informacji o graczu
  59. function getUserInfo($id, $login, $server, $accessToken)
  60. {
  61.     if (empty($id) && !empty($login))
  62.     {
  63.         $api = "profileByLogin";
  64.         $search = $login;
  65.     }
  66.     elseif (!empty($id) && empty($login))
  67.     {
  68.         $api = "profile";
  69.         $search = $id;
  70.     }
  71.     else
  72.     {
  73.         return json_encode(array("error" => "Nie ma takiego heheszkowania!")); 
  74.     }
  75.    
  76.     $header = array(
  77.         sprintf('token: %s', $accessToken)
  78.       );
  79.  
  80.     $curl = curl_init("http://". $server .".e-sim.org/mobile/".$api."/" . $search);
  81.     curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  82.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  83.     curl_setopt($curl, CURLOPT_POST, false);
  84.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  85.     $result = curl_exec($curl);
  86.     curl_close($curl);
  87.  
  88.    
  89.     if(empty($result))
  90.         return json_encode(array("error" => "No such citizen"));
  91.    
  92.     return $result;
  93. }
  94.  
  95.  
  96.  
  97. ?>
Advertisement
Add Comment
Please, Sign In to add comment