我试图通过获取meetup数据。
events/
我使用以下代码来获取数据:
// Open a stream in read-only mode
if (!($stream = fopen("http://stream.meetup.com/2/open_events", 'r'))) {
die('Could not open stream for reading');
}
// Check if the stream has more data to read
while (!feof($stream)) {
// Read 1024 bytes from the stream
$data= fread($stream, 1024);
echo '<pre>';
echo ($data);
}
// Be sure to close the stream resource when you're done with it
fclose($stream);
exit;上面的代码是返回结果的,数据是json格式的,然后我必须解码它。我可以用php 'json_decode‘函数来解码它。但是我面临的问题是,我已经接收到json对象的数据,有的时候是完整的对象,有时是一半的对象,而这些对象在php中是无法解码的。
对我的代码或其他代码示例的任何帮助都是非常有用和感谢的。
发布于 2013-11-05 13:42:04
这个问题很可能是
fread($stream, 1024);如果JSON比这1024字节长,那么就会得到被破坏的对象。要么增加长度,要么使用没有length参数的fgets,或者使用这个稍微短一些的选项:
$stream = new SplFileObject("http://stream.meetup.com/2/open_events");
while (!$stream->eof()) {
var_dump(json_decode($stream->fgets()));
}https://stackoverflow.com/questions/19789435
复制相似问题