Curl/Json doesnt work on wordpress (2 posts)

  1. halluzineyt
    Posted 2 years ago #

            // Supply a user id and an access token
            $userid = "****";
            $accessToken = "*****";
            // Gets our data
            function fetchData($url){
                 $ch = curl_init();
    			 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                 curl_setopt($ch, CURLOPT_URL, $url);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                 curl_setopt($ch, CURLOPT_TIMEOUT, 20);
                 $result = curl_exec($ch);
                 return $result;
            // Pulls and parses data.
            $result = fetchData("https://api.instagram.com/v1/users/$userid/media/recent/?access_token=$accessToken");
            $result = json_decode($result);
    <?php if(!empty($result->data)): ?>
        <?php foreach ($result->data as $post): ?>
            <!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
            <a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
        <?php endforeach ?>
    <?php endif ?>

    Im trying to add a separate php code to pull instagram photos to wordpress then integrate it with gridrotator.js. This code works on some online compiler but it doesnt seem to be working on wordpress do you know why?

  2. Anfisa
    Posted 1 year ago #

    Try to change

    $result = fetchData("https://api.instagram.com/v1/users/$userid/media/recent/?access_token=$accessToken");

    to this:

    $result = file_get_contents("https://api.instagram.com/v1/users/$userid/media/recent/?access_token=$accessToken");

    and this
    <?php foreach ($result->data as $post): ?>

    to this:

    <?php foreach ((array) $result->data as $post): ?>

    This solution worked for me.

Topic Closed

This topic has been closed to new replies.

About this Topic