WordPress.org

Ready to get started?Download WordPress

Forums

Make curl/json work on wordpress (4 posts)

  1. halluzineyt
    Member
    Posted 3 months ago #

    <?php
    
      function fetchData($url){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_TIMEOUT, 20);
      $result = curl_exec($ch);
      curl_close($ch);
      return $result;
      }
    
      $result = fetchData("https://api.instagram.com/v1/users/**/media/recent/?access_token=******.*******&count=60");
    
      $result = json_decode($result);
      foreach ($result->data as $post) {
         if(empty($post->caption->text)) {
           // Do Nothing
         }
         else {
            echo '<a class="instagram-unit" target="blank" href="'.$post->link.'">
            <img src="'.$post->images->low_resolution->url.'" alt="'.$post->caption->text.'" width="50" height="auto" />
            </a>';
         }
    
      }
    ?>

    I got this bit of code to pull instagram images, it works on some online compiler but i cant make it work on wordpress any idea how?

  2. I am not too familiar with cURL.. but I think the HTTP API from the Codex is more of what you want to do.

  3. halluzineyt
    Member
    Posted 3 months ago #

    <?php
    
    $result = wp_remote_get( "https://api.instagram.com/v1/users/***********/media/recent/?access_token=**********6" );
    
      $result = json_decode($result);
      foreach ($result->data as $post) {
         if(empty($post->caption->text)) {
           // Do Nothing
         }
         else {
            echo '<a class="instagram-unit" target="blank" href="'.$post->link.'">
            <img src="'.$post->images->low_resolution->url.'" alt="'.$post->caption->text.'" width="50" height="auto" />
            </a>';
         }
    
      }
    ?>

    i tried this still doesnt work/

  4. halluzineyt
    Member
    Posted 3 months ago #

    <?php
    
      $result = wp_remote_get("https://api.instagram.com/v1/users/*********/media/recent/?access_token=*********&count=60", true);
    	$decode = json_decode($result['body'], true);
    
      foreach ($decode->data as $post) {
         if(empty($post->caption->text)) {
           // Do Nothing
         }
         else {
            echo '<a class="instagram-unit" target="blank" href="'.$post->link.'">
            <img src="'.$post->images->low_resolution->url.'" alt="'.$post->caption->text.'" width="50" height="auto" />
            </a>';
         }
    
      }
    ?>

    Can you check this one?

Reply

You must log in to post.

About this Topic