Reading JSON result with PHP

Asked

Viewed 381 times

1

I have the following result in JSON and need to treat it with PHP.

JSON:

    {
  "name": "abc",
  "count": 6,
  "frequency": "Manual Crawl",
  "version": 3,
  "newdata": true,
  "lastrunstatus": "success",
  "thisversionstatus": "success",
  "thisversionrun": "Thu Jul 09 2015 11:46:39 GMT+0000 (UTC)",
  "results": {
    "collection2": [
      {
        "property11": {
          "href": "http://click.uol.com.br/?rf=meio-ambiente_home-headline-especiais-2colunas-1_1&u=http://noticias.uol.com.br/meio-ambiente/ultimas-noticias/redacao/2015/07/08/lei-mexicana-faz-com-que-tigres-e-leoes-sejam-vendidos-a-preco-de-banana.htm",
          "text": "Lei mexicana faz com que tigres e leões sejam vendidos a \"preço de banana\""
        }
      },

PHP:

 <?php
$request = "https://www.kimonolabs.com/api/5bwvka8s?apikey=*****&kimmodify=1";
$response = file_get_contents($request);
$results = json_decode($response, TRUE);

foreach($results['results']['collection2'] as $collection) {
    echo "<a href='" . $collection['property11']['href'] ."'>" . $collection['property11']['text'] . "</a><br >";
}



?>

Problem solved!

  • You could post your full json?

  • 1

    Shouldn’t be echo of $results instead of $response?

  • I tested here and gave anyway, appears all data JSON. #@Sergio

  • 1

    What gives var_dump($results);?

  • 4

    collection2 is a vector, $Results['Results']['collection2'][0]['property11']['text']

  • @Gustavocave ask the question the result of echo and also of a var_dump of the variable $response. So help in the answers...

  • I got through the answer below Hiago . I thank you all!

Show 2 more comments

2 answers

3

The problem is that collection2 will become an array (at least in the example of this URL you passed)

Then try to scroll through this array

Ex.

<?php

$result = file_get_contents("https://www.kimonolabs.com/api/5bwvka8s?apikey=****&kimmodify=1");

$json = json_decode($result, true);

foreach($json['results']['collection2'] as $collection) {
    echo $collection['property11']['text'];
    echo "<br>";
}

2


In the example you entered the collection2 is a vector, so as you passed an "Indice" that does not exist it will not bring the text.

Note: You are also printing the wrong variable, follow below the solution

<?php

$request = "https://www.kimonolabs.com/api/5bwvka8s?apikey=****&kimmodify=1";
$response = file_get_contents($request);
$results = json_decode($response, TRUE);


    echo $results ['results']['collection2'][0]['property11']['text'];



?>

The vector value [0] may contain more than one result, so consider using a foreach to traverse all

Example

<?php
$request = "https://www.kimonolabs.com/api/5bwvka8s?apikey=****&kimmodify=1";
$response = file_get_contents($request);
$results = json_decode($response, TRUE);

foreach($results['results']['collection2'] as $collection) {
    echo $collection['property11']['text']."<br />";
}
  • Thankful Hiago, it worked.

Browser other questions tagged

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