0

For some reason, when we we're on another server (Tierpoint) everything worked fine and we recently switched over to WP Engine and the transients that I was using for my module have stopped working. Below, I will go into detail on both without and with transients.

WITHOUT Transients:

<?php
    /* Call the cURL request to pull in Instagram images */
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/v5.0/". get_option('insta_id') ."/media?fields=media_url,permalink,username,media_type,thumbnail_url&access_token=". get_option('insta_accesstoken'));
    $result = curl_exec($curl);
    $array = json_decode($result, true);
?>

<?php
/* Loop through the array and only pull API fields */
$mediaUrls = array_map(function($entry) {
    return [
        'media_url' => $entry['media_url'],
        'permalink' => $entry['permalink'],
        'username' => $entry['username'],
        'media_type' => $entry['media_type'],
        'thumbnail_url' => !empty($entry['thumbnail_url']) ? $entry['thumbnail_url'] : ""
    ];
}, $array['data']);
?>

Media loads on every refresh of the website (Other html elements have not been included) - Works great:
enter image description here


WITH Transients:

<?php $cached_result = get_transient('instagram');

if(empty($cached_result)): ?>
    <?php
    /* Call the cURL request to pull in Instagram images */
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/v5.0/". get_option('insta_id') ."/media?fields=media_url,permalink,username,media_type,thumbnail_url&access_token=". get_option('insta_accesstoken'));
    $result = curl_exec($curl);
    $array = json_decode($result, true);
    set_transient('instagram', $result, 8 * HOUR_IN_SECONDS);
    ?>
<?php else: ?>
    <?php return $cached_result; ?>
<?php endif; ?>

<?php
/* Loop through the array and only pull API fields */
$mediaUrls = array_map(function($entry) {
    return [
        'media_url' => $entry['media_url'],
        'permalink' => $entry['permalink'],
        'username' => $entry['username'],
        'media_type' => $entry['media_type'],
        'thumbnail_url' => !empty($entry['thumbnail_url']) ? $entry['thumbnail_url'] : ""
    ];
}, $array['data']);
?>

It writes to the database: enter image description here

.. but all that I get back is a blank screen as shown below: enter image description here

Is this a caching issue? Am I not doing something right with the transient?

  • Since this issue only happens on WP-Engine, I would highly recommend contacting their support team. They're the best people to investigate and will have access to adjust any configuration needed. – WebElaine Nov 22 '19 at 21:39
  • Unfortunately, it looks like they don't handle code issues (which is what their claiming) - It has to be a caching issue.. hmm. – SLE Nov 22 '19 at 21:48
  • In the else, shouldn't there be a $array = $cached_result; ? Why the return there? – Sally CJ Nov 22 '19 at 22:03
  • 1
    json_decode( $cached_result, true ) should work. – Sally CJ Nov 23 '19 at 1:56
  • 1
    @SallyCJ, FIXED! Worked like a champ, thanks so much! Post it as the answer if you'd like so I can upvote. – SLE Nov 23 '19 at 2:04
2

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; ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.