Cakephp Twitch.Tv API加载时间
我正在使用以下代码:Cakephp Twitch.Tv API加载时间,php,json,cakephp,Php,Json,Cakephp,我正在使用以下代码: public function getUser($name) { $return = array(); $file = array(); $httpSocket = new HttpSocket(); $url = $this->baseUrl . $name . $this->apiKey; $temp = $httpSocket->get($url); $f
public function getUser($name) {
$return = array();
$file = array();
$httpSocket = new HttpSocket();
$url = $this->baseUrl . $name . $this->apiKey;
$temp = $httpSocket->get($url);
$file = $temp->body;
$file = explode(',', $file);
$i = 0;
foreach ($file as $info) {
$info = str_replace("{", "", $info);
$info = str_replace("}", "", $info);
$info = str_replace('"', "", $info);
$info = str_replace("[", "", $info);
$info = str_replace("]", "", $info);
$temp = explode(':', $info, 2);
if ($temp[0] == 'stream') {
$temp[1] = str_replace("game:", "", $temp[1]);
if ($temp[1] == 'StarCraft II: Wings of Liberty') {
$temp[1] = 'starcraft-II';
}
$return[$i]['game'] = $temp[1];
} elseif ($temp[0] == 'teams') {
$temp[1] = str_replace("name:", "", $temp[1]);
if ($temp[1] != '') {
$return[$i]['teams'] = $temp[1];
} else {
$return[$i]['teams'] = null;
}
} else {
if (isset($temp[1])) {
$return[$i][$temp[0]] = $temp[1];
}
}
}
return $return;
}
我想知道我是否可以做些什么来减少这个脚本的加载时间。我正在从TwitchTV中提取一个json文件以作记录。仅在页面刷新/加载时,该功能就非常有效。页面呈现中存在明显的2-3秒延迟。一如既往,我们非常感谢您的帮助。您知道API的响应是json吗?为什么要手动解析响应而不是使用json_decode()?使用json_decode()并查看它将生成什么。您可以删除90%的代码 此外,我会根据您的需要,将API响应缓存几分钟到几个小时。阅读本书的缓存一章,页面应该在刷新时立即加载,因为它不需要进行另一个API调用