manaluni

Twitch json problem

May 15th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.29 KB | None | 0 0
  1. if (!empty($_GET)) {
  2.     $url = "https://api.twitch.tv/kraken/oauth2/token";
  3.     $data = Array(
  4.         "client_id" => "#",
  5.         "client_secret" => "#",
  6.         "grant_type" => "authorization_code",
  7.         "redirect_uri" => "http://localhost/twitchTest",
  8.         "code" => $_GET['code'],
  9.         "state" => ""
  10.     );
  11.  
  12.     $data_string = "";
  13.     foreach ($data as $key => $value) {
  14.         $data_string .= $key . '=' . $value . '&';
  15.     }
  16.     $data_string = substr_replace($data_string, '', -1);
  17.  
  18.     try {
  19.         $ch = curl_init();
  20.         if ($ch === false) {
  21.             throw new Exception("Failed to initialize");
  22.         }
  23.         curl_setopt($ch, CURLOPT_URL, $url);
  24.         curl_setopt($ch, CURLOPT_POST, count($data));
  25.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  26.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  27.  
  28.         $result = curl_exec($ch);
  29.         if ($result === false) {
  30.             throw new Exception(curl_error($ch), curl_errno($ch));
  31.         }
  32.         curl_close($ch);
  33.         var_dump($result);
  34.         $gebruiker = json_decode($result, true);
  35.         var_dump($gebruiker);
  36.         } catch (Exception $e) {
  37.         trigger_error(sprintf("Curl failed with error #%d: %s", $e->getCode(), $e->getMessage()), E_USER_ERROR);
  38.     }    
  39. }
Add Comment
Please, Sign In to add comment