我可以请求 JSON 字符串的一部分吗?或者仅解码特定部分以加快进程?
我的页面执行速度非常慢,因为它是通过 JSON 请求加载的。
所以我想知道如何才能加快整个过程。我可以只请求字符串的一部分吗?考虑到我在下面的数组中使用的是“cast”数组,必须获取所有这些数组是一种浪费。
下面的代码必须为我的页面上列出的每部电影运行,目前是 20 部。但在减少电影数量之前,我想知道如何加快这部分的速度,这才是正确的问题。
这是我得到的代码,它会减慢页面速度,因为它必须运行 20 次。
$films_result = $tmdb->getMovie($film->id);
$films = json_decode($films_result);
foreach ($films as $film) {
foreach ($film->cast as $cast) {
if ($cast->job == 'Actor') {
echo '<a href="person.php?id=' . $cast->id . '">' . $cast->name . '</a> ';
$num_actors++;
if ($num_actors == 5)
break;
}
}
}
我环顾四周,但我不知道我在寻找什么。我在这方面还是个初学者。所以请任何解决方案不涉及任何其他技术,只涉及 PHP。
I've got a very slow performing page, because it's loaded down by JSON requests.
So I was wondering how I can speed the whole thing up. Can I request only part of the string? Considering all I'm using from the following one is the "cast" array, it's a waste to have to get all of it.
The following code has to run for every movie listed on my page, which at the moment is 20. But before just cutting the number of movies down, I was wondering how I could speed this part up, which is the proper problem.
Here's the code I've got that slows the page down, as it has to run 20 times.
$films_result = $tmdb->getMovie($film->id);
$films = json_decode($films_result);
foreach ($films as $film) {
foreach ($film->cast as $cast) {
if ($cast->job == 'Actor') {
echo '<a href="person.php?id=' . $cast->id . '">' . $cast->name . '</a> ';
$num_actors++;
if ($num_actors == 5)
break;
}
}
}
I've had a look around, but I don't know what I'm looking for. I'm very very much a beginner at this. So please could any solutions not involve any other technologies, just PHP please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于禁用了压缩,您的 API 在 Google 页面速度方面获得 0 分(共 100 分)。
启用压缩后,您的 JSON 输出加载速度将加快 95%。
如果您无法控制服务器,则可能很难修复,但这里有一个链接如何修复它。
最小化负载
Your API gets 0 of 100 points in google page speed because the compression is disabled.
Your JSON output will load 95% faster with compression enabled.
It might be hard to fix if you are not in control of the server but here's a link how to fix it anyways.
Minimize payload
您可以设置一个 cron 作业,每 X 分钟在幕后抓取所有 JSON(这样无论多慢),然后解析它并将其存储在数据库中。然后,每次加载上面显示的 PHP 页面时,只需从数据库中获取缓存数据并输出即可。
You could setup a cron job that grabs all the JSON every X minutes behind the scenes (that way it doesn't matter how slow it is), then parse it and store it in a database. Then each time you load the PHP page you've shown above, just grab the cached data from the database and output that.
您应该能够在不到一秒的时间内解析大量 JSON x 20。你怎么知道 JSON 是瓶颈?可能还有其他问题。从数据库检索数据的速度有多快?
You should be able to parse a lot of JSON x 20 in less than a second. How do you know that the JSON is the bottle neck? There's probably some other problem. How quickly is the data retrieved from the database?
您假设 JSON 解码是缓慢的原因:我敢打赌(少量)金钱是 API 响应时间是接收器,而不是解析。
假设它需要解析,你真的希望有某种类型的流解析器:在Java领域有json-simple(参见http://code.google.com/p/json-simple/wiki/DecodingExamples#Example_5_-_Stoppable_SAX-like_content_handler)使用处理程序并自己跟踪状态,这样您就只需保留您关心的位即可。不幸的是,PHP 并没有真正公开
json_decode
使用的解析器回调,并且不再真正支持任何旧的 JSON 编码器/解码器(无论如何,它们中的任何一个都不公开对用户代码的回调)。如果它确实是一个约束,您可能会考虑将 JSON 转储到客户端并在那里解析它。
You're assuming the JSON decoding is the cause of the slowness: I'd bet (a small amount of) money on the API response time being the sink, not the parsing.
Assuming it IS need the parsing, you're really hoping for a streaming parser of some kind: in Java land there is json-simple (see http://code.google.com/p/json-simple/wiki/DecodingExamples#Example_5_-_Stoppable_SAX-like_content_handler) which lets you use a handler and track the state yourself so you only bother keeping the bits you care about. Unfortunately PHP doesn't really expose the parser callbacks that
json_decode
uses, and none of the older JSON encoders/decoders are really supported any more (not that any of them exposed callbacks to user code anyway).You might consider dumping the JSON out to the client and parsing it there instead, if it is really a constraint.