I think "Content from an API" guide needs a revisit

I decided to dig into the guide Content from an API | Kirby CMS, but it’s become outdated, as the NYT movie review API has been shut down. They suggest an alternate way to access movie reviews, but I can’t quite get it to work.

That may be enough info right there — but FWIW, here’s what I’ve tried:


Per the NYT Article Search | Dev Portal, I edited the request on line 16 of models/reviews.php :

$request = Remote::get('https://api.nytimes.com/svc/search/v2/articlesearch.json?fq=section_name%3A"Movies"&type_of_material%3A"Review"&sort=newest&page=0&api-key=' . $apiKey);

(The request string alone does work in the browser and returns movie article data, though it doesn’t appear limited to reviews.)

I further edited some of the query fields to what’s available per the raw JSON returns and the list at Article Search | Dev Portal:

foreach ($results as $key => $review) {
	$pages[] = [
		'slug'     => Str::slug($review->headline), /* changed from 'display_title' */
		'num'      => $key+1,
		'template' => 'review',
		'model'    => 'review',
		'content'  => [
			/* 'title'  => $review->display_title,   -- not available */
			'headline' => $review->headline->main,
			'byline'   => $review->byline->original,
			'lead_paragraph' => $review->summary_short, /* changed from 'summary' */
			'pub_date' => $review->publication_date, /* changed from 'date' */
			'web_url'  => $review->link->url, /* changed from 'link' */
			'snippet' => $review->link->suggested_link_text, /* changed from `linkText` */
			'cover'    => $review->multimedia->2->url,
			'uuid'     => Uuid::generate(),

		]
	];
}

This got me the error:
Undefined property: stdClass::$results

The results seem(?) to be called “response” now, so I tried editing line 18 of models/reviews.php:

if ($request->code() === 200) {
    $results = $request->json(false)->response; /* changed to response from results */
}

This gets farther, but no matter what I do I get some variety of the error:
Attempt to read property "headline" on array

And that’s where I run out of obvious ideas. If I’m missing something simple, a hint would be appreciated so I can continue, but the Guide article should probably get a refresh…

for example, is an array, you are using it as string.

Guess you have json decoded to objects instead of array?

$reviews = $request->json(true);
dump($reviews['response']['docs']);

Thanks for the clues, @texnixe, but I guess I need to find somewhere to learn prerequisites. (If there’s Kirby documentation on consuming JSON strings vs. arrays, I haven’t spotted it.)

I tried some changes based on your suggestion and could only get either Attempt to read property “headline” on string or Attempt to read property “headline” on array.

It’s my first foray into API stuff… :man_shrugging:

As someone famous once said…