将分页添加到Instagram Search API [AJAX + PHP]

时间:2015-04-13 22:34:08

标签: php ajax pagination instagram instagram-api

stackoverflow上的第一篇文章! 使用此脚本的修改版http://www.hongkiat.com/blog/instagram-photo-search/

我已将其更改为用户搜索而不是标记搜索..添加两个api调用一个用于ID,然后另一个用于图像用户名..

我已经看到其他使用分页的例子.. 但不确定如何将其应用到我当前的代码 ..使用Instagram API相当新的

随意使用我的代码!这是每个用户检索照片的非常好的ajax方式。 我取出了CURL部分以获得可读性。

我只想每页打20张照片。的感谢。

instasearch.php

<?php
header('Content-type: application/json');
define("YOUR_TOKEN", 'TOKEN HERE');
$query = $_POST['q'];
$userid = 'https://api.instagram.com/v1/users/search?count=1&q=' . $query . '&access_token='. YOUR_TOKEN;
$clnum = mt_rand(1,3);

// function get_curl($url) WOULD GO HERE //

// SEARCH FOR ID //
$response = get_curl($userid);

if ($response){
    foreach(json_decode($response)->data as $item){     
        $id = $item->id;

        $user[] = array(
        "id" => htmlspecialchars($id),

        );
   }
}

// SEARCH BY ID //
$api ='https://api.instagram.com/v1/users/' . $id . '/media/recent/?' . '&access_token='. YOUR_TOKEN . '&count=33';

$response2 = get_curl($api);
$images = array();

if($response2){
    foreach(json_decode($response2)->data as $item){        
        $src = $item->images->standard_resolution->url;
        $thumb = $item->images->thumbnail->url;
        $url = $item->link;
        $count = $item->likes->count;

        $images[] = array(
        "src" => htmlspecialchars($src),
        "thumb" => htmlspecialchars($thumb),
        "url" => htmlspecialchars($url),
        "count" => htmlspecialchars($count)
        );
    }
}

print_r(str_replace('\\/', '/', json_encode($images)));

//if($likes)
//print_r(json_encode($likes));
//
die();
?>

ajax.js

$(document).ready(function(){
    var sfield = $("#s");
    var container = $("#photos");
    var timer;

    function instaSearch() {
        $(sfield).addClass("loading");
        $(container).empty();
        var q = $(sfield).val();

        $.ajax({
            type: 'POST',
            url: 'http://192.168.0.3/igpanel/image-select/instasearch.php',
            data: "q="+q,
            success: function(data){
                $(sfield).removeClass("loading");

                $.each(data, function(i, item) {
                    var ncode = '<span class="p"><span id="likes"><i style="color:#E74C3C" class="fa fa-heart"></i> '+data[i].count+' likes</span><!--<a href="'+data[i].url+'" target="_blank">--><img id="'+data[i].url+'" src="'+data[i].thumb+'"></a></span>';
                    $(container).append(ncode);
                });
            },
            error: function(xhr, type, exception) { 
                $(sfield).removeClass("loading");
                $(container).html("Error: " + type); 
            }
        });
    }

HTML

<section id="sform">
    <input type="text" id="s" name="s" class="form-control" placeholder="Enter Username..." autocomplete="off">
    </section>
    <div id="image_container">
    <section id="photos"></section>
    </div>

0 个答案:

没有答案