如何从具有JSON源的页面中提取所需数据?

时间:2014-07-24 01:20:18

标签: php file-get-contents

例如,这是QR图像:

enter image description here

Source

This API解码QR并输出它的内容,在这种情况下是“HelloWorld”。

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

1 个答案:

答案 0 :(得分:0)

实际上可以使用file_get_contents()来访问它。 ( 测试并正常工作

<?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())以根据需要回显这些内容。

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

或者简单地说:

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

这将为您提供所需的:HelloWorld