问题
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