duan0714 2014-07-16 10:22
浏览 48
已采纳

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

I need the feed to provide seven images at a time. For some reason, all pages provide seven except the first page which only provides 5.

<?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 } ?>

I have played around with all kinds of settings, if $maxtagid is set, removing the VERIFYPEER, etc. and nothing seems to work. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douweibiao8471 2014-07-16 10:32
    关注

    I think the max_id parameter is causing issue on first page.. try this solution:

    <?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 } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电脑出问题了,说是usbmom注册表没
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网