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, ".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:
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, ".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:
.. but all that I get back is a blank screen as shown below:
Is this a caching issue? Am I not doing something right with the transient?
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/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:
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/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:
.. but all that I get back is a blank screen as shown below:
Is this a caching issue? Am I not doing something right with the transient?
Share Improve this question asked Nov 22, 2019 at 21:23 user155484user155484 9 | Show 4 more comments1 Answer
Reset to default 1So 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; ?>
else
, shouldn't there be a$array = $cached_result;
? Why thereturn
there? – Sally CJ Commented Nov 22, 2019 at 22:03json_decode( $cached_result, true )
should work. – Sally CJ Commented Nov 23, 2019 at 1:56