New twitch API get games by name working?

I can retrieve games by id but I get a 400 bad request when I try to get them using a name string. The names I’ve been trying have come directly from the name field of the request when I do it by id so there shouldn’t be an issue with matching.

using php the function is : (with TWITCH_API_ID defined eariler)

function getTwitchGameDataByName($gameName){
	// using new TWITCH API
	try{
		$url = "https://api.twitch.tv/helix/games?name=$gameName";
		$ch = curl_init();
		$headers=array('Client-ID: '.TWITCH_API_ID);
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		$result = curl_exec ($ch);
		//echo $result;
		curl_close ($ch);
		if (!empty($result)){
		return json_decode($result, true);}			
	}
	catch (Exception $ex){
		
		return NULL;
	}
}

if you want to test it use the parameter
$gameName = “Company of Heroes: Tales of Valor”;
Also ‘Company of Heroes 2’ doesn’t work but strangely ‘RimWorld’ and ‘HomeWorld’ do is there a space issue here ?

Ok after lots and lots of searching it turns out the spaces in the game names are an issue.

I solved this in php by using the function urlencode(string)

The following code works for games with spaces in such as Company of Heroes and Company of Heroes 2

function getTwitchGameDataByName($gameName){
	// using new TWITCH API
	try{
		$url = "https://api.twitch.tv/helix/games?name=".urlencode($gameName);
		$ch = curl_init();
		$headers=array('Client-ID: '.TWITCH_API_ID);
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		$result = curl_exec ($ch);
		curl_close ($ch);
		if (!empty($result)){
		return json_decode($result, true);}			
	}
	catch (Exception $ex){
		
		return NULL;
	}
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.