Authentication: token valid = false

I was going to. I guess I’ll also have to figure out how to get user id now. Can you take a look what’s going on with my token? Why it’s not valid? I uploaded live example:
http://phantomik.ru/catalog/ - press “Войти” in the top right corner. What will happen step by step:

  1. This link will open in popup and ask for your permissions:
    https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=dkh8hc1goz4d1jkc4ekivziur4a8kq0&redirect_uri=http://phantomik.ru/oauth&scope=user_read+chat_login
  2. After you agree, Twitch will redirect inside that popup to this page, providing your code in GET:
    http://phantomik.ru/oauth?code=f8mlpl4z30yh169cdqqc22xk7v7d5t&scope=user_read+chat_login
  3. On this page I placed PHP script that will get code argument and pass it with cURL (see function above) to Twitch API on this url:
    https://api.twitch.tv/kraken/?oauth_token='.$_GET['code'].'&client_id=dkh8hc1goz4d1jkc4ekivziur4a8kq0
  4. After getting answer from Twitch API it will output answer inside that popup with PHP function var_export(). I made this just for test purposes.

Here’s function that I use to pass auth token to Twitch on step 3. I’ve added application/vnd.twitchtv.v5+json header to use API v5.

function cURL($url, $p=''){
	$ch =  curl_init();
	$options = array(
		CURLOPT_URL => $url,
		CURLOPT_HTTPHEADER => array('application/vnd.twitchtv.v5+json'),
		CURLOPT_SSL_VERIFYHOST => 0,
		CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)',
		CURLOPT_REFERER => 'http://phantomik.ru',
		CURLOPT_RETURNTRANSFER => 1,
		CURLOPT_SSL_VERIFYPEER => 0,
	);
	if ($p) {
		$options[CURLOPT_CUSTOMREQUEST] = "POST";
		$options[CURLOPT_POST] = 1;
		$options[CURLOPT_POSTFIELDS] = $p;
	}
	curl_setopt_array($ch , $options);

	$result = curl_exec($ch);
	$errorno = curl_errno($ch);
	$error = curl_error($ch);
	curl_close($ch);
	if ($errorno) {
		return array('error' => $errorno, 'result' => 'cURL Error: '.$error);
	} else {
		return json_decode($result);
	}
}