English 中文(简体)
4. 处理APIC(JSON + PHP)
原标题:Dealing with Instagram APIs (JSON + PHP)

I m new to PHP and I can t figure out how to deal with Instagram APIs in order to (for example) extract a list of links to the standard resolution images by recent 3 items published by the user_id 3.

在我迄今创造的今天:

<?php
function get_instagram($user_id,$count)
{
    $user_id =  3 ;
    $count =  3 ;
    $url =  https://api.instagram.com/v1/users/ .$user_id. /media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count= .$count;
    $jsonData = $json_decode((file_get_contents($url)));
    $data = $jsonData->data;
    $result =  <ul> ;
    foreach ($data as $key => $value) {
        $result .=  <li><a href= .$value->link.  ><img src=" .$value->images->standard_resolution->url. " width="70" height="70" /></a></li>  ;
    }
    $result .=  </ul> ;
    return $result;
}

结果是,你能够帮助我?

最佳回答

你们需要重复数据或做一些事情。 (因此,在您的json_decode功能前有>。)

www.un.org/spanish/ecosoc Try this:

<?php
function get_instagram($user_id=15203338,$count=6,$width=190,$height=190){

    $url =  https://api.instagram.com/v1/users/ .$user_id. /media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count= .$count;

    // Also Perhaps you should cache the results as the instagram API is slow
    $cache =  ./ .sha1($url). .json ;
    if(file_exists($cache) && filemtime($cache) > time() - 60*60){
        // If a cache file exists, and it is newer than 1 hour, use it
        $jsonData = json_decode(file_get_contents($cache));
    } else {
        $jsonData = json_decode((file_get_contents($url)));
        file_put_contents($cache,json_encode($jsonData));
    }

    $result =  <div id="instagram"> .PHP_EOL;
    foreach ($jsonData->data as $key=>$value) {
        $result .= "	". <a class="fancybox" data-fancybox-group="gallery" 
                            title=" .htmlentities($value->caption->text).   .htmlentities(date("F j, Y, g:i a", $value->caption->created_time)). "
                            style="padding:3px" href=" .$value->images->standard_resolution->url. ">
                          <img src=" .$value->images->low_resolution->url. " alt=" .$value->caption->text. " width=" .$width. " height=" .$height. " />
                          </a> .PHP_EOL;
    }
    $result .=  </div> .PHP_EOL;
    return $result;
}

echo get_instagram();
?>


With $value->location->name

如果你想要检查其空洞,如果是这样的话,那么你也想对它作一个扼杀,如果你会这样做的话:

$location = (!empty($value->location->name))? @ .$value->location->name:null;

然后使用<代码>$,以便在你希望时加以反映。

问题回答

I ve extended the selected answer by writing a small script that can be used a boilerplate for anyone trying this.

它包含所有变量,并首先使用基本标记。 你们可以利用从APICA/6中挑选出来的人,能够根据需要在其产出的基础上再接再厉。

This is built to be used specifically with WordPress with one small tweak to the image size conditional it can be used in any PHP application.

<?php
function instagram($count = 10, $width = 640, $height = 640) {

    $user_id = 123456789;
    $access_token =  123456789 ;
    $size = wp_is_mobile() ?  low_resolution  :  standard_resolution ;
    $url =  https://api.instagram.com/v1/users/ .$user_id. /media/recent/?access_token= .$access_token. &count= .$count;
    $cache_location =  ./ .sha1($url). .json ;
    $cache_time =  -1 hour ;

    if (file_exists($cache_location) && filemtime($cache_location) > strtotime($cache_time)) {
        // If a cache file exists, and it is newer than 1 hour, use it
        $jsonData = json_decode(file_get_contents($cache_location));
    } else {
        $jsonData = json_decode((file_get_contents($url)));
        file_put_contents($cache_location,json_encode($jsonData));
    }

    foreach ($jsonData->data as $key=>$value) {
        echo  <div> ;
        echo  <img src=" .$value->images->$size->url. "/> ;
        echo  </div> ;
    }
}

Basic Usage

<?php echo instagram(); ?>

先进使用

<?php echo instagram($count = 1, $width = 100, $height = 100); ?>

rel=“nofollow>> Github Repo

Welp, 这在一周前就开始工作,但现在给我一个PHP错误:

"Message: Trying to get property of non-object"

我的JSON饲料实际上被腐化,我的一些摇篮的帽子缺失,因此我不得不补充一句空话。 我现在还使用一种不同的方法,根据你们的思想,大事!

<?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/USER ID HERE/media/recent/?access_token=ACCES TOKEN HERE&count=14");

  $cache =  ./instagram.json ;
  if(file_exists($cache) && filemtime($cache) > time() - 60*60){
  // If a cache file exists, and it is newer than 1 hour, use it
  $instaData = json_decode(file_get_contents($cache));
    } else {
  $instaData = json_decode((file_get_contents($result)));
    file_put_contents($cache,json_encode($instaData));
  }

  foreach ($instaData->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="100%" height="auto" />
        <div class="instagram-desc"> .htmlentities($post->caption->text).  |  .htmlentities(date("F j, Y, g:i a", $post->caption->created_time)). </div></a> ;
     }

  }
?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签