I have a custom widget that will display the public feed from instagram profiles using the json response from the site. On my dev machine all is working fine but on tophost where the final wordpress installation is hosted, I have always a php error:
Invalid argument supplied for foreach() on line 60
I've checked the line, and it's relative to the processing of the returned array of data from Instagram, but it's strange because as I wrote there is no error on my local machine.
here is the code:
public function widget( $args, $instance )
{
$url = 'https://instagram.com/'.$instance['username'].'/?__a=1';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Accept: application/json') );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
$feed = curl_exec($ch);
curl_close($ch);
$data = json_decode( $feed , true);
ob_start();
?>
<div class="jumbotron jumbotron-fluid ig-feed">
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="swiper-container swiper-feed">
<div class="swiper-wrapper">
<?php
// this is the foreach() that is generating the error
foreach( $data['graphql']['user']['edge_owner_to_timeline_media']['edges'] as $node ){
foreach( $node as $img ){
echo '<div class="swiper-slide feed-img" style="background-image:url('.$img['display_url'].');"></div>';
}
}
?>
</div>
<div class="swiper-button-prev "></div>
<div class="swiper-button-next"></div>
</div>
</div>
</div>
</div>
</div>
<?php
echo ob_get_clean();
}
I've checked the provider website and curl is enabled. Can be the problem in the csp I set on the function.php file?
function http_headers()
{
header("Strict-Transport-Security: max-age=31536000; includeSubDomains");
header("Set-Cookie: HttpOnly;Secure");
# header("Content-Security-Policy: default-src 'self' 'unsafe-inline' https://www.youtube.com/; script-src 'self' 'unsafe-inline' https://www.youtube.com; img-src 'self' https://scontent-mxp1-1.cdninstagram.com; style-src 'self' 'unsafe-inline' https://scontent-mxp1-1.cdninstagram.com; font-src 'self' data:; object-src 'none';");
header("X-Frame-Options: SAMEORIGIN");
header("X-Xss-Protection: 1; mode=block");
header("X-Content-Type-Options: nosniff");
header("Referrer-Policy: strict-origin");
header("X-Pingback: ");
header("X-Powered-By: ");
}
add_action('send_headers', 'http_headers');
Thanks for the help.
NB: The server is running on php 7.2 under CGI/FastCGI