Is It Possible To Receive Only Partial Results In Json From Api Calls Using Php?
Solution 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
Solution 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.
Solution 3:
No. You need to fetch the whole set before parsing and sending the data you need back to the client machine.
Post a Comment for "Is It Possible To Receive Only Partial Results In Json From Api Calls Using Php?"