is it possible to receive only partial results in JSON from API calls using PHP?

送分小仙女□ 提交于 2021-01-28 06:25:39

问题


I have been using PHP to call an API and retrieve a set of data in JSON.I need to make 20 batch calls at once. Therefore, speed is very important.

However, I only need the first element, in fact, I only need one data which is right at the beginning of the JSON file, index 0. However, the JSON file returns about 300 sets of data. I have to wait until all the data are ready before I can proceed.

I want to speed up my API calls by eliminating redundant datasets. Is it possible for me to receive the first set of the data only without having to wait until everything is ready and then go indexing the first element?

excuse my english...thank you in advance.


回答1:


you could use fopen to get the bytes that are guaranteed to have what you need in it and then use regex to parse it. something like:

$max_bytes = 512;
$fp = fopen($url, "r") ;

$data = "" ;
if($fp) {

    while(!preg_match('/"totalAmount"\:"(.*)"/U', $data, $match)) 
        $data .= stream_get_contents($fp, $max_bytes) ;

    fclose($fp);

    if(count($match)){
        $totalAmount = $match[1];
    }
}

keep in mind that you cant use the string stored in $data as valid json. It will only be partial




回答2:


no. json is not a "streamable" format. until you receive the whole string, it cannot be decoded into a native structure. if you KNOW what you need, then you could use string operations to retrieve the portion you need, but that's not reliable nor advisable. Similarly, php will not stream out the text as it's encoded.

e.g. consider a case where your data structure is a LOOOONG shallow array

$x = array(
    0 => blah
    1 => blah
    ...
    999,999,998 => blah
    999,999,999 => array( .... even more nested data here ...)
);

a streaming format would dribble this out as

['blah', 'blah' ............

you could assume that there's nothing but those 'blah' at the top level and output a ], to produce a complete json string:

['blah'....   , 'blah']

and send that, but then you continue encoding and reach that sub array... now you've suddenly got

['blah' ....., 'blah'][ ...sub array here ....]

and now it's no longer valid JSON.

So basically, json encoding is done in one (long) shot, and not in dibs and drabs, just because you simply cannot know what's coming "later" without parseing the whole structure first.




回答3:


No. You need to fetch the whole set before parsing and sending the data you need back to the client machine.



来源:https://stackoverflow.com/questions/15210739/is-it-possible-to-receive-only-partial-results-in-json-from-api-calls-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!