0

So I have some relatively simple JSON I'm trying to display using PHP and I'm getting stuck, I think perhaps I'm not using decode or encode correctly. Maybe I simply overlooked something.

Here's the JSON...

{

   "numFound": 43640,

   "start": 0,

   "maxScore": 0.7847167,

   "docs": [],

   "facets": {}

}

Here's my PHP...

<?php
$json_returned = file_get_contents("URL_OF_JSON_SOURCE");
$decoded_results = json_decode($array, true);

{
  foreach($decoded_results as $results){
 echo "Number Found:".$results['numFound'].";
 echo "Start:".$results['start'].";

  }

}

 ?>

I'm primarily just trying to get "numFound", "start", and "maxScore" to display. Thanks for any help, or even taking the time to read this post.

Here's the source JSON.. https://api.data.gov/gsa/fbopen/v0/opps?q=technology&data_source=FBO&limit=1&show_closed=true&api_key=CTrs3pcYimTdR4WKn50aI1GcUxyL9M4s1fyBbSer

John Chase
  • 105
  • 8

2 Answers2

0

You don't have any JSON Array in the given data. So firstly you don't have to loop returned data. Secondly you just forget double quotes in the loop. Third you don't have to join strings if you got any of them is null.

Here is the solution :

<?php
$result = json_decode( file_get_contents("sth"), true );
echo 'Number Found :'.$result["numFound"].'<br/>';
echo 'Start :'.$result["start"].'<br/>';
aprogrammer
  • 1,764
  • 1
  • 10
  • 20
  • Works kind of but displays values as 0, here's the link to what it's supposed to return... – John Chase Apr 24 '16 at 23:04
  • https://api.data.gov/gsa/fbopen/v0/opps?q=cat&data_source=FBO&limit=1&show_closed=true&api_key=CTrs3pcYimTdR4WKn50aI1GcUxyL9M4s1fyBbSer – John Chase Apr 24 '16 at 23:05
0

You php code is messed up

<?php
$json_returned = file_get_contents("https://api.data.gov/gsa/fbopen/v0/opps?q=technology&data_source=FBO&limit=1&show_closed=true&api_key=CTrs3pcYimTdR4WKn50aI1GcUxyL9M4s1fyBbSer");
$decoded_results = json_decode($json_returned, true);

echo "Number Found:".$decoded_results['numFound']." ";
echo "Start:".$decoded_results['start'];
?>
Cesar Bielich
  • 4,754
  • 9
  • 39
  • 81