As i said in the title i am using through php the json api for twitch tv and own3d tv to get the information of the stream i want.
Ths problem is that the page is not loading fast, as a matter of fact some times the php server stops because of 30 or more secs of function.
The error : Fatal error: Maximum execution time of 30 seconds exceeded in
I am using an online indicator :
function status($stream_id, $type){
if($type == 't'){
$chan = "http://api.justin.tv/api/stream/list.json?channel=" . $stream_id;
$json = file_get_contents($chan);
$exist = strpos($json, $stream_id);
if($exist) {
return true;
}else{
return false;
}
}else if($type == 'o'){
$url = 'http://api.own3d.tv/liveCheck.php?live_id=' . $stream_id;
$xml = simplexml_load_file($url);
$isLive=$xml->liveEvent->isLive;
if ($isLive == "true") {
return true;
}else{
return false;
}
}
}
and i am using a function that get some info from the stream :
function api_stream_data($stream_id, $type){
$stream_id = sanitize($stream_id);
$type = sanitize($type);
if($type == 't'){
$streamData = json_decode(file_get_contents("http://api.justin.tv/api/stream/list.json?channel=$stream_id"),true);
$data = array(
'image'=>$streamData[0]['channel']['image_url_medium'],
'title'=>$streamData[0]['title'],
'limage'=>$streamData[0]['channel']['screen_cap_url_huge'],
'game'=>$streamData[0]['meta_game']
);
}else if($type == 'o'){
$streamData = json_decode(file_get_contents("http://api.own3d.tv/rest/live/list.json?liveid=$stream_id"),true);
$data = array(
'image'=>$streamData[0]['thumbnail_small'],
'title'=>$streamData[0]['live_name'],
'limage'=>$streamData[0]['thumbnail_large'],
'game'=>$streamData[0]['game_name']
);
}
return $data;
}
All functions works perfectly but the problem is the time they get to excecute….
Is there any possible way to do that faster??
I have seen some other site examples that are loading very fast like http://www.solomid.net and http://www.clgaming.net .
Thanks in advance for any help!
EDIT: *SOLVED* Thank’s each and every one of you for your help! I used a cronjob that stores the data to the database and then i just made a query to request them, it updates every 5 mins but, oh well, better than nothing.
Your code is slow becuase you are doing one http request per user. Just one http request usually takes arround 200ms-400ms. PHP is not asynchronous, this means that is going to stop the execution of your code until it gets a response from own3d or twitch.
I’m not familiar with the own3d api, but in Twitch you can send all the users id you want and it will fetch you all the details in one single http request.
This is how I’m doing the fetching of twitchtv live streams on my site.
Example:
$response = $client->get(”, ”, ‘http://api.justin.tv/api/stream/list.json?channel=’.implode(‘,’, $users));
This would generate the following url:
http://api.justin.tv/api/stream/list.json?channel=IPLLoL,tsm_theoddone,KungenTV
This way you can fetch all your user’s streams in one single http request.
I’ll suggest that since you are already identifying to which stream provider the stream belongs, instead of doing the http request, create an array, one for each stream provider and add the stream to the corresponding one.
In the end make an http request, one for all the streams on the own3d array, and one for all the streams in the twitchtv array. Now you’ll have all your user’s streams with only two http requests.
Oh and another thing. Do not do this stream fetching stuff when you are rendering your site, I have all this streams fetching stuff in ajax, that way, my page will load first and then it will start doing the http request after my site has been rendered.
Also caching the response that twitch and own3d sends you is a good idea. That way you will not be fetching data for own3d and twitch on every user that visits your site. This is great speacially if you have some solid traffic.
That should really improve the speed. I hope this would help.