I am trying to pull movie reviews from the USA Times. I am still a newbie at PHP and this is driving me crazy. I have been working this for more than a week now and can not figure it out.
Any suggestions would be great.
I am able to connect, pull the data, and do the
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $apiRequest); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $cresult = curl_exec($curl); curl_close($curl); $cdata = json_decode($cresult, true); echo ("curl results:"),'<br />'; echo $cresult; echo ("end curl"),'<br />';
This returns the following:
curl results: {"APIParameters":{"Count":"1","MinimumRating":"","MaximumRating":"","Actor":"","Movie":"","Director":"","MPAARating":"","Year":""},"Found":1,"Movies":null,"Actors":null,"Directors":null,"MovieReviews":[{"MovieName":"Snitch","ActorName":"Dwayne Johnson, Susan Sarandon, Jon Bernthal, Barry Pepper, Benjamin Bratt","ReleaseDate":"","Rating":"3","Director":"Ric Roman Waugh","Distributor":"Summit Entertainment","MPAARating":"Rated PG-13","Reviewer":"Claudia Puig","Brief":null,"Review":"\u003cp\u003eDrama and suspense fuel a surprisingly effective thriller, more than car chases and action.\u003c/p\u003e","ReviewDate":"2/21/2013 6:43:51 PM","WebUrl":"http://apidata.usatoday.com/story/life/movies/2013/02/21/snitch-review/1921585/?kjnd=JbPObtXsOdWL9gAh55Q6rHUAwsnktpji2GmaoiSpssYpIbvC7pn8S4POyQMm5y5E-4a605af0-b70c-425d-be7a-bf6e6332fc3e_YPNGs3E%2FdTm%2Bx6eBoZ2UH0rq6YrNqBght%2Bd0UMQH9YiS5EWn%2FTJD8rKl%2FRltfQrL","Id":850575}]}end curl
Then to display the actual data from the API:
if ($query != 0) { echo '<h2 class="mainHeading">Results of your search for <strong>'.$query.'</strong> (Showing '.$query.' out of a possible '.$query.' matches)</h2>'."\n"; echo '<ol>'."\n"; foreach($cdata['APIParameters'] as $item){ $movien = (string)$item['MovieName']; echo $movien; var_dump($item['Movie'].'<br />');
} echo '</ol>'."\n"; echo '<p class="control"><a href="'.basename(__FILE__).'" class="refresh">Reset the page</a></p>'."\n"; $i = $i + 1; } else { echo '<h2 class="mainHeading">No results for <strong>'.$query.'</strong>.</h2>'."\n"; echo '<p class="control"><a href="'.basename(__FILE__).'" class="refresh">Reset the page</a></p>'."\n"; }
This gives me the following errors: Results of your search for 1 (Showing 1 out of a possible 1 matches) Warning: Illegal string offset 'MovieName' in C:\wamp2\www\websearch\USASearch2.php on line 173 1 Warning: Illegal string offset 'Movie' in C:\wamp2\www\websearch\USASearch2.php on line 175 string(7) "1 " Warning: Illegal string offset 'MovieName' in C:\wamp2\www\websearch\USASearch2.php on line 173 Notice: Uninitialized string offset: 0 in C:\wamp2\www\websearch\USASearch2.php on line 173 Warning: Illegal string offset 'Movie' in C:\wamp2\www\websearch\USASearch2.php on line 175 Notice: Uninitialized string offset: 0 in C:\wamp2\www\websearch\USASearch2.php on line 175 string(6) " " Warning: Illegal string offset 'MovieName' in C:\wamp2\www\websearch\USASearch2.php on line 173