WordPress transients not rendering

Question

Could someone tell me what I might be doing wrong with my below cURL + Transient call? When the page loads the first time, it pulls in all the cURL data and then on refresh it’s not loading anymore.

Picture of the database:
enter image description here

<?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/v4.0/". get_option('insta_id') ."/media?fields=media_url,permalink,username,thumbnail_url&access_token=". get_option('insta_accesstoken'));
    $result = curl_exec($curl);
    $array = json_decode($result, true);
    set_transient('instagram', $array, 8 * HOUR_IN_SECONDS);
    ?>
<?php else: ?>
    <?php return $cached_result; ?>
<?php endif; ?>

the weird part is.. when I use the below code, I have no problems at all but I wanted to see if there was a more efficient way like shown above. Here is the code that works 100%:

<?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/v4.0/". get_option('insta_id') ."/media?fields=media_url,permalink,username,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 $array = json_decode($cached_result, true); ?>
<?php endif;  ?>

Could someone tell me what I might be doing wrong on the top cURL request? The bottom one works flawlessly.

0
, , SaintLouis Events 2 weeks 0 Answers 20 views 0

Leave an answer