通过PHP中的meetup开放事件实时流获取Meetup事件数据

阿桑·阿斯兰

我正在尝试通过meetup开放事件流API获取meetup事件数据。

http://www.meetup.com/meetup_api/docs/stream/2/open_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中解码。

对我的代码或其他代码示例的任何帮助都将非常有帮助和感激。

高登

问题很可能

fread($stream, 1024);

如果JSON的长度超过了1024个字节,则将导致对象损坏。要么增加长度,要么使用fgets没有长度参数的方法,或者使用以下较短的替代方法:

$stream = new SplFileObject("http://stream.meetup.com/2/open_events");
while (!$stream->eof()) {
    var_dump(json_decode($stream->fgets()));
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章