Skip to main content
Improved wording
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

So I don't know whatwhy the return $cached_result; here does or why it worked before for youin the else, but if that's intentional, then you shouldn'tshould probably do:

return json_decode( $cached_result, true );

Because you're caching the response body (which is a JSON string) and you're reading the non-cached result like so: $array = json_decode($result, true);.

But if that return therewas a mistake, then this part:

<?php else: ?>
    <?php return $cached_result; ?>
<?php endif; ?>

Instead, that shouldShould be written as:

<?php else: ?>
    <?php $array = json_decode( $cached_result, true ); ?>
<?php endif; ?>

Because the code that follows has $array['data']:

<?php
/* Loop through the array and only pull API fields */
$mediaUrls = array_map(function($entry) {
    ...
}, $array['data']);
?>

Additionally, you should read the cached result the same way you read the non-cached result; i.e.:

// Reading the non-cached result.
$array = json_decode($result, true);

// And reading the cached result.
$array = json_decode($cached_result, true);

But if you actually did mean to return the data, then perhaps you should do return json_decode( $cached_result, true ); ..

I don't know what the return here does or why it worked before for you, but you shouldn't return there:

<?php else: ?>
    <?php return $cached_result; ?>
<?php endif; ?>

Instead, that should be written as:

<?php else: ?>
    <?php $array = json_decode($cached_result, true); ?>
<?php endif; ?>

Because the code that follows has $array['data']:

<?php
/* Loop through the array and only pull API fields */
$mediaUrls = array_map(function($entry) {
    ...
}, $array['data']);
?>

Additionally, you should read the cached result the same way you read the non-cached result; i.e.:

// Reading the non-cached result.
$array = json_decode($result, true);

// And reading the cached result.
$array = json_decode($cached_result, true);

But if you actually did mean to return the data, then perhaps you should do return json_decode( $cached_result, true ); ..

So I don't know why the return $cached_result; in the else, but if that's intentional, then you should probably do:

return json_decode( $cached_result, true );

Because you're caching the response body (which is a JSON string) and you're reading the non-cached result like so: $array = json_decode($result, true);.

But if that return was a mistake, then this part:

<?php else: ?>
    <?php return $cached_result; ?>
<?php endif; ?>

Should be written as:

<?php else: ?>
    <?php $array = json_decode( $cached_result, true ); ?>
<?php endif; ?>
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

I don't know what the return here does or why it worked before for you, but you shouldn't return there:

<?php else: ?>
    <?php return $cached_result; ?>
<?php endif; ?>

Instead, that should be written as:

<?php else: ?>
    <?php $array = json_decode($cached_result, true); ?>
<?php endif; ?>

Because the code that follows has $array['data']:

<?php
/* Loop through the array and only pull API fields */
$mediaUrls = array_map(function($entry) {
    ...
}, $array['data']);
?>

Additionally, you should read the cached result the same way you read the non-cached result; i.e.:

// Reading the non-cached result.
$array = json_decode($result, true);

// And reading the cached result.
$array = json_decode($cached_result, true);

But if you actually did mean to return the data, then perhaps you should do return json_decode( $cached_result, true ); ..