Advertisement
Kenningar

Test Api

Nov 14th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.69 KB | None | 0 0
  1. <?php
  2. function do_webauth($method, $username, $password) {
  3.     $cookie_file = 'cookie.txt'; // a valid file path to where the cookies will be stored (ie cookie.txt)
  4.     $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
  5.  
  6.     $methods = array(
  7.         'psn' => 'Psnid',
  8.         'xbox' => 'Xuid'//'Wlid'
  9.     );
  10.     $dest = 'Wlid'; if (isset($methods[$method])) $dest = $methods[$method];
  11.     $url = BUNGIE_URL.'/fr/User/SignIn/'.$dest;
  12.  
  13.     $default_options = array(
  14.         CURLOPT_USERAGENT => $user_agent,
  15.         CURLOPT_COOKIEJAR => $cookie_file,
  16.         CURLOPT_COOKIEFILE => $cookie_file,
  17.         CURLOPT_RETURNTRANSFER => true,
  18.         CURLOPT_SSL_VERIFYHOST => 2,
  19.     );
  20.  
  21.     // Get Third Party Authorization URL
  22.     $ch = curl_init();
  23.     curl_setopt_array($ch, $default_options);
  24.     curl_setopt_array($ch, array(
  25.         CURLOPT_URL => $url,
  26.     ));
  27.     curl_exec($ch);
  28.     $redirect_url = curl_getinfo($ch)['redirect_url'];
  29.     curl_close($ch);
  30.  
  31.     // Bungie Cookies are still valid
  32.     if (!$redirect_url) return true;
  33.  
  34.     // Try to authenticate with Third Party
  35.     $ch = curl_init();
  36.     curl_setopt_array($ch, $default_options);
  37.     curl_setopt_array($ch, array(
  38.         CURLOPT_URL => $redirect_url,
  39.     ));
  40.     $auth_result = curl_exec($ch);
  41.     $auth_info = curl_getinfo($ch);
  42.     $auth_url = $auth_info['redirect_url'];
  43.  
  44.     // Normally authentication will produce a 302 Redirect, but Xbox is special...
  45.     if ($auth_info['http_code'] == 200) $auth_url = $auth_info['url'];
  46.  
  47.     curl_close($ch);
  48.  
  49.     // No valid cookies
  50.     if (strpos($auth_url, $url.'?code') !== 0) {
  51.         $result = false;
  52.         switch($method) {
  53.             case 'psn':
  54.                 $login_url = 'https://auth.api.sonyentertainmentnetwork.com/login.do';
  55.  
  56.                 // Login to PSN
  57.                 $ch = curl_init();
  58.                 curl_setopt_array($ch, $default_options);
  59.                 curl_setopt_array($ch, array(
  60.                     CURLOPT_URL => $login_url,
  61.                     CURLOPT_POST => 3,
  62.                     CURLOPT_POSTFIELDS => http_build_query(array(
  63.                         'params' => 'cmVxdWVzdF9sb2NhbGU9ZW5fVVMmcmVxdWVzdF90aGVtZT1saXF1aWQ=', // without empty server result
  64.                         'j_username' => $username,
  65.                         'j_password' => $password,
  66.                         'rememberSignIn' => 1 // Remember signin
  67.                     )),
  68.                 ));
  69.                 curl_exec($ch);
  70.                 $redirect_url = curl_getinfo($ch)['redirect_url'];
  71.                 curl_close($ch);
  72.  
  73.                 if (strpos($redirect_url, 'authentication_error') !== false) return false;
  74.  
  75.                 // Authenticate with Bungie
  76.                 $ch = curl_init();
  77.                 curl_setopt_array($ch, $default_options);
  78.                 curl_setopt_array($ch, array(
  79.                     CURLOPT_URL => $redirect_url,
  80.                     CURLOPT_FOLLOWLOCATION => true
  81.                 ));
  82.                 curl_exec($ch);
  83.                 $result = curl_getinfo($ch);
  84.                 curl_close($ch);
  85.                 break;
  86.             case 'xbox':
  87.                 $login_url = 'https://login.live.com/ppsecure/post.srf?'.substr($redirect_url, strpos($redirect_url, '?')+1);
  88.                 preg_match('/id\="i0327" value\="(.*?)"\//', $auth_result, $ppft);
  89.  
  90.                 if (count($ppft) == 2) {
  91.                     $ch = curl_init();
  92.                     curl_setopt_array($ch, $default_options);
  93.                     curl_setopt_array($ch, array(
  94.                         CURLOPT_URL => $login_url,
  95.                         CURLOPT_POST => 3,
  96.                         CURLOPT_POSTFIELDS => http_build_query(array(
  97.                             'login' => $username,
  98.                             'passwd' => $password,
  99.                             'KMSI' => 1, // Stay signed in
  100.                             'PPFT' => $ppft[1]
  101.                         )),
  102.                         CURLOPT_FOLLOWLOCATION => true
  103.                     ));
  104.                     $auth_result = curl_exec($ch);
  105.                     $auth_url = curl_getinfo($ch)['url'];
  106.                     curl_close($ch);
  107.  
  108.                     if (strpos($auth_url, $url.'?code') === 0) {
  109.                         return true;
  110.                     }
  111.                 }
  112.                 return false;
  113.                 break;
  114.         }
  115.         $result_url = $result['url'];
  116.         if ($result['http_code'] == 302) $result_url = $result['redirect_url'];
  117.  
  118.         // Account has not been registered with Bungie
  119.         if (strpos($result_url, '/Register') !== false) return false;
  120.  
  121.         // Login successful, "bungleatk" should be set
  122.         // Facebook/PSN should return with ?code=
  123.         // Xbox should have ?wa=wsignin1.0
  124.         return strpos($result_url, $url) === 0;
  125.     }
  126.     // Valid Third Party Cookies, re-authenticating Bungie Login
  127.     $ch = curl_init();
  128.     curl_setopt_array($ch, $default_options);
  129.     curl_setopt_array($ch, array(
  130.         CURLOPT_URL => $auth_url,
  131.     ));
  132.     curl_exec($ch);
  133.     curl_close($ch);
  134.     return true;
  135. }
  136.  
  137. $method = 'xbox';
  138. $username = '';
  139. $password = '';
  140.  
  141.  
  142. $resultat = do_webauth($method,$username,$password);
  143.  
  144. var_dump($resultat);
  145.  
  146.  
  147. $apiKey = 'mykey';
  148.  
  149. $ch = curl_init();
  150. curl_setopt($ch, CURLOPT_URL, 'https://www.bungie.net/Platform/Destiny/1/MyAccount/Character/2305843009241535752/Vendor/3003633346/?lc=fr&definitions=true');
  151. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  152. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-Key: ' . $apiKey));
  153.  
  154. $vendor = json_decode(curl_exec($ch));
  155.  
  156. $table = (array) $vendor->Response;
  157.  
  158. echo "<pre>";
  159. var_dump($table);
  160. echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement