collect data json print php

Asked

Viewed 654 times

0

People like I get the data in php of latitude and longitude of this url below

https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/0ozWRQqxiMnv5bqJzSUIMyUIIbMGrP5qu/message.json

1 answer

0


You can use the function file_get_contents(). It would look something like:

$conteudo = file_get_contents('https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/0ozWRQqxiMnv5bqJzSUIMyUIIbMGrP5qu/message.json');
echo $conteudo;

Since it is a JSON, we can transform it into an object using the function json_decode($conteudo). If you want to see the exact JSON format you can use the following code

echo '<pre>';
print_r($conteudo);
echo '</pre>';

Now that you already have the content in an object, we can make a foreach to scroll through all JSON content

foreach ($conteudo->response->feedMessageResponse->messages->message as $key) {
    print_r('Latitude: '.$key->latitude);
    echo ' - ';
    print_r('longitude: '.$key->longitude);
    echo '<br>';
}

The whole code went like this then:

$conteudo = json_decode(file_get_contents('https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/0ozWRQqxiMnv5bqJzSUIMyUIIbMGrP5qu/message.json'));
foreach ($conteudo->response->feedMessageResponse->messages->message as $key) {
    print_r('Latitude: '.$key->latitude);
    echo ' - ';
    print_r('longitude: '.$key->longitude);
    echo '<br>';
}
  • Phelipe and that’s right I just don’t know now and catch latitude and longitude rsrs

  • I complemented my answer. Now it takes the values you wanted. Just understand the structure of JSON and go through it.

  • Perfect friend of mine

  • can take me only one more doubt as I would to catch only the last latitude and longitude ?

  • can take me only one more doubt as I would to catch only the last latitude and longitude ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.