How can I extract the required data from a page with JSON it's source? [closed]

谁说胖子不能爱 提交于 2019-12-13 09:14:59

问题


For example, here is a QR Image:

Source

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

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


回答1:


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);

?>

Which returns this object:

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

                )

        )

)

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

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

Or simply:

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

And that will give you the desired: HelloWorld



来源:https://stackoverflow.com/questions/24923658/how-can-i-extract-the-required-data-from-a-page-with-json-its-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!