bmwhite4 Posted March 14, 2013 Share Posted March 14, 2013 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 1731Warning: Illegal string offset 'Movie' in C:\wamp2\www\websearch\USASearch2.php on line 175string(7) "1"Warning: Illegal string offset 'MovieName' in C:\wamp2\www\websearch\USASearch2.php on line 173Notice: Uninitialized string offset: 0 in C:\wamp2\www\websearch\USASearch2.php on line 173Warning: Illegal string offset 'Movie' in C:\wamp2\www\websearch\USASearch2.php on line 175Notice: Uninitialized string offset: 0 in C:\wamp2\www\websearch\USASearch2.php on line 175string(6) ""Warning: Illegal string offset 'MovieName' in C:\wamp2\www\websearch\USASearch2.php on line 173 Quote Link to comment https://forums.phpfreaks.com/topic/275661-json-api-php-array-help/ Share on other sites More sharing options...
AyKay47 Posted March 14, 2013 Share Posted March 14, 2013 If you output: echo "<pre>"; print_r($cdata); echo "</pre>"; You will get a better idea of the array structure. Quote Link to comment https://forums.phpfreaks.com/topic/275661-json-api-php-array-help/#findComment-1418644 Share on other sites More sharing options...
bmwhite4 Posted March 14, 2013 Author Share Posted March 14, 2013 OK, did that and I get: Array ( [APIParameters] => Array ( [Count] => 1 [MinimumRating] => [MaximumRating] => [Actor] => [Movie] => [Director] => [MPAARating] => [Year] => ) [Found] => 1 [Movies] => [Actors] => [Directors] => [MovieReviews] => Array ( [0] => Array ( [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] => [Review] => Drama and suspense fuel a surprisingly effective thriller, more than car chases and action. [ReviewDate] => 2/21/2013 6:43:51 PM [WebUrl] => http://apidata.usatoday.com/story/life/movies/2013/02/21/snitch-review/1921585/?kjnd=sjYkpHRx36xdI9nIUF4zSCO%2FS2NYJcmTa6F9PLrc24q0%2Bl7arQI9wplXLuRz3SUd-b0f6a17a-8cfd-42f3-8fb5-cbde99aef18b_skg4aYQUcUFeiN%2BIahUNjdRHHad1jo%2FUFxTimTSnEfYiZaZvuPCsFTZ5xdvlyL8G [id] => 850575 ) ) ) I have been trying to test by getting the movie name first. IT is the MovieName that I am trying to pull. I have no idea what I am doing wrong here. Here is the warning from $movien = (string)$item['MovieName']; Warning: Illegal string offset 'MovieName' in C:\wamp2\www\websearch\USASearch2.php on line 174 This is what I get from echo $movien 1 This is from:var_dump($item['Movie'].'<br />');Warning: Illegal string offset 'Movie' in C:\wamp2\www\websearch\USASearch2.php on line 176string(7) "1" I still just do not know. Quote Link to comment https://forums.phpfreaks.com/topic/275661-json-api-php-array-help/#findComment-1418653 Share on other sites More sharing options...
bmwhite4 Posted March 14, 2013 Author Share Posted March 14, 2013 I also tried changing to: $movien = $item['MovieName']; echo $movien, '<br /><br />'; and did a var_dump($movien). I still get the errors Warning: Illegal string offset 'MovieName' in C:\wamp2\www\websearch\USASearch2.php on line 1741 The var_dump gives me this: string(1) "1", That leads me to believe that I am trying to access it the wrong way. When I change it to this I get this error. Parse error: syntax error, unexpected ''APIParameters'' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in C:\wamp2\www\websearch\USASearch2.php on line 173 Quote Link to comment https://forums.phpfreaks.com/topic/275661-json-api-php-array-help/#findComment-1418659 Share on other sites More sharing options...
us2rn4m2 Posted March 14, 2013 Share Posted March 14, 2013 // Movie foreach($array as $k => $v) { if(is_array($v) && array_key_exists('Movie', $v)) { echo $v['Movie']; } } echo $array['APIParameters']['Movie']; // MovieName foreach($array['MovieReviews'] as $k => $v) { echo $v['MovieName']; } echo $array['MovieReviews'][0]['MovieName']; Quote Link to comment https://forums.phpfreaks.com/topic/275661-json-api-php-array-help/#findComment-1418673 Share on other sites More sharing options...
bmwhite4 Posted March 14, 2013 Author Share Posted March 14, 2013 // Movie foreach($array as $k => $v) { if(is_array($v) && array_key_exists('Movie', $v)) { echo $v['Movie']; } } echo $array['APIParameters']['Movie']; // MovieName foreach($array['MovieReviews'] as $k => $v) { echo $v['MovieName']; } echo $array['MovieReviews'][0]['MovieName']; Can you tell me what the as $k=> $v does? When I change to that I get this new error. Parse error: syntax error, unexpected '$k' (T_VARIABLE) in ... Quote Link to comment https://forums.phpfreaks.com/topic/275661-json-api-php-array-help/#findComment-1418678 Share on other sites More sharing options...
salathe Posted March 14, 2013 Share Posted March 14, 2013 You're almost there, just looping over the wrong thing. foreach ($cdata['MovieReviews'] as $movie_review) { echo $movie_review['MovieName']; } I find it useful to see the JSON in a more structured way. Tools like JSONLint, or browser addons like JSONView will reformat plain JSON into a more pretty layout. JSONView Similarly, in PHP it is often nice to see the structure of an array. For that, make sure you output plain text (e.g. header('Content-Type: text/plain')) rather than HTML, which will eat up your pretty whitespace unless you use something like a <pre> tag. var_export() Quote Link to comment https://forums.phpfreaks.com/topic/275661-json-api-php-array-help/#findComment-1418683 Share on other sites More sharing options...
Solution bmwhite4 Posted March 14, 2013 Author Solution Share Posted March 14, 2013 You're almost there, just looping over the wrong thing. foreach ($cdata['MovieReviews'] as $movie_review) { echo $movie_review['MovieName']; } I find it useful to see the JSON in a more structured way. Tools like JSONLint, or browser addons like JSONView will reformat plain JSON into a more pretty layout. JSONView Similarly, in PHP it is often nice to see the structure of an array. For that, make sure you output plain text (e.g. header('Content-Type: text/plain')) rather than HTML, which will eat up your pretty whitespace unless you use something like a <pre> tag. var_export() Thank you that helped like a charm!! I appriciate all the info and help. Quote Link to comment https://forums.phpfreaks.com/topic/275661-json-api-php-array-help/#findComment-1418688 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.