如何从具有JSON来源的页面中提取所需数据? [英] How can I extract the required data from a page with JSON it's source?

查看:125
本文介绍了如何从具有JSON来源的页面中提取所需数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,这是一个QR图片:

For example, here is a QR Image:

This API decodes the QR and outputs it's content which is "HelloWorld" in this case.

如何使用file_get_contents()或类似的功能来获取所需的数据.

How can I use file_get_contents() or a similar function to fetch the required data.

推荐答案

实际上可以使用file_get_contents()进行访问. ( 经过测试且可以正常工作 )

Actually is is possible to use file_get_contents() to access it. (tested AND working)

<?php 

$url = 'http://api.qrserver.com/v1/read-qr-code/?fileurl=http%3A%2F%2Fapi.qrserver.com%2Fv1%2Fcreate-qr-code%2F%3Fdata%3DHelloWorld';

$stuff = file_get_contents($url);
$data = json_decode($stuff);

print_r($data);

?>

哪个返回此对象:

Array
(
    [0] => stdClass Object
        (
            [type] => qrcode
            [symbol] => Array
                (
                    [0] => stdClass Object
                        (
                            [seq] => 0
                            [data] => HelloWorld
                            [error] => 
                        )

                )

        )

)

允许您遍历(使用foreach())以根据需要回显内容.

Allowing you to loop through (using foreach()) to echo out the stuff as you require.

foreach($data as $item) {
    echo $item->symbol[0]->data;
}

或者简单地:

echo $data[0]->sybmol[0]->data;

这将为您提供所需的内容:HelloWorld

And that will give you the desired: HelloWorld

这篇关于如何从具有JSON来源的页面中提取所需数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆