Instagram API - 在第一页上没有返回正确数量的图像

时间:2014-07-16 10:22:57

标签: php instagram

我需要Feed一次提供七张图片。出于某种原因,除了仅提供5的第一页之外,所有页面都提供了7个页面。

<?php
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);
    curl_close($ch); 
    return $result;
}

$maxtagid = (isset($_GET['instatag'])) ? $_GET['instatag'] : false;
$url = "https://api.instagram.com/v1/users/self/feed?access_token=$accesstoken&count=7";
if($maxtagid){
    $url .= "&max_id=$maxtagid";
}
$result = json_decode(fetchData($url)); 
foreach ($result->data as $post) echo '<div class="insta-square"><a target="_blank" href="'.$post->link.'"><img src="'.$post->images->standard_resolution->url.'" alt="'.$post->caption->text.'" width="100%" height="auto" /></a></div>';
$nextmax = $result->pagination->next_max_id;
if($nextmax != ''){ ?>
    <div class="insta-square"><a href="<?php echo get_home_url(); ?>/gallery?instatag=<?php echo $nextmax; ?>"><img src="<?php echo get_template_directory_uri(); ?>/images/next-slide-square.png" /></a></div>
<?php } ?>

我玩过各种各样的设置,如果设置了$ maxtagid,删除了VERIFYPEER等,似乎没什么用。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为max_id参数导致首页出现问题。请尝试此解决方案:

<?php
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);
    curl_close($ch); 
    return $result;
}

$maxtagid = (isset($_GET['instatag'])) ? $_GET['instatag'] : false;
$url = "https://api.instagram.com/v1/users/self/feed?access_token=$accesstoken&count=7";
if($maxtagid){
     $url .= "&max_id=$maxtagid";
}
$result = json_decode(fetchData($url));
foreach ($result->data as $post) echo '<div class="insta-square"><a target="_blank" href="'.$post->link.'"><img src="'.$post->images->standard_resolution->url.'" alt="'.$post->caption->text.'" width="100%" height="auto" /></a></div>';
$nextmax = $result->pagination->next_max_id;
if($nextmax != ''){ ?>
    <div class="insta-square"><a href="<?php echo get_home_url(); ?>/gallery?instatag=<?php echo $nextmax; ?>"><img src="<?php echo get_template_directory_uri(); ?>/images/next-slide-square.png" /></a></div>
<?php } ?>