Instagram API - 如何获取最近标记的媒体列表
Instagram API - how to get a list of recently tagged media
嗨社区我真的很新,我正在尝试在 instagram 上搜索 post(这个应用程序将在我的网络应用程序中使用),我已经搜索了这几天的所有答案,并尝试了大多数社区提供的所有代码。而且,这对我来说是死胡同。
这是我的完整代码
1.home.php
<head>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<div id="w">
<section id="sform">
<input type="text" id="s" name="q" class="sfield" placeholder="Enter a search tag..." autocomplete="off">
</section>
<section id="photos"></section>
</div>
</body>
2.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: 'instasearch.php',
data: "q="+q,
success: function(data){
$(sfield).removeClass("loading");
$.each(data, function(i, item) {
var ncode = '<div><h1>'+data[i].name+'</h1><p>'+data[i].mediacount+'</p></div>';
$(container).append(ncode);
});
},
error: function(xhr, type, exception) {
$(sfield).removeClass("loading");
$(container).html("Error: " + type);
}
});
}
$(sfield).keydown(function(e){
if(e.keyCode == '32' || e.keyCode == '188' || e.keyCode == '189' || e.keyCode == '13' || e.keyCode == '190' || e.keyCode == '219' || e.keyCode == '221' || e.keyCode == '191' || e.keyCode == '220') {
e.preventDefault();
} else {
clearTimeout(timer);
timer = setTimeout(function() {
instaSearch();
}, 900);
}
});
});
3.instasearch.php
header('Content-Type: application/json');
$client = "xxxxx";
$access_token = "xxxxxx";
$query = $_POST['q'];
$api = 'https://api.instagram.com/v1/tags/search?q='.$query.'&access_token='.$access_token.'';
function get_curl($url) {
if(function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
return $output;
} else {
return file_get_contents($url);
}
}
$response = get_curl($api);
$datang = array();
if($response){
foreach(json_decode($response)->data as $item){
$mediacount = $item->media_count;
$name = $item->name;
$datang[] = array(
"mediacount" => htmlspecialchars($mediacount),
"name" => htmlspecialchars($name),
);
}
}
print_r(str_replace('\/', '/', json_encode($datang)));
die();
输出没问题,目前..
下一步,我想改变这个
$api = 'https://api.instagram.com/v1/tags/search?q='.$query.'&access_token='.$access_token.'';
进入这个
$api = "https://api.instagram.com/v1/tags/'.$query.'/media/recent?access_token=".$access_token."";
它 return 一个错误(从我的网络浏览器复制粘贴)
{
"pagination": {
"deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead"
},
"meta": {
"code": 200
},
"data": []
}
请社区,我该如何解决这个问题?有人吗?
我想像这里提到的那样输出(至少JSON)https://instagram.com/developer/endpoints/tags/
GET /tags/{tag-name}/media/recent..
感谢社区。
我觉得你的问题出在js
var sfield = $("#s");
var container = $("#photos");
您将 jquery 元素存储在变量中,稍后您在 jquery 元素上调用 jquery。将 $(sfield) 替换为 sfield。也为 container
这样做
此问题已解决!
嗨社区我真的很新,我正在尝试在 instagram 上搜索 post(这个应用程序将在我的网络应用程序中使用),我已经搜索了这几天的所有答案,并尝试了大多数社区提供的所有代码。而且,这对我来说是死胡同。 这是我的完整代码
1.home.php
<head>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<div id="w">
<section id="sform">
<input type="text" id="s" name="q" class="sfield" placeholder="Enter a search tag..." autocomplete="off">
</section>
<section id="photos"></section>
</div>
</body>
2.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: 'instasearch.php',
data: "q="+q,
success: function(data){
$(sfield).removeClass("loading");
$.each(data, function(i, item) {
var ncode = '<div><h1>'+data[i].name+'</h1><p>'+data[i].mediacount+'</p></div>';
$(container).append(ncode);
});
},
error: function(xhr, type, exception) {
$(sfield).removeClass("loading");
$(container).html("Error: " + type);
}
});
}
$(sfield).keydown(function(e){
if(e.keyCode == '32' || e.keyCode == '188' || e.keyCode == '189' || e.keyCode == '13' || e.keyCode == '190' || e.keyCode == '219' || e.keyCode == '221' || e.keyCode == '191' || e.keyCode == '220') {
e.preventDefault();
} else {
clearTimeout(timer);
timer = setTimeout(function() {
instaSearch();
}, 900);
}
});
});
3.instasearch.php
header('Content-Type: application/json');
$client = "xxxxx";
$access_token = "xxxxxx";
$query = $_POST['q'];
$api = 'https://api.instagram.com/v1/tags/search?q='.$query.'&access_token='.$access_token.'';
function get_curl($url) {
if(function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
return $output;
} else {
return file_get_contents($url);
}
}
$response = get_curl($api);
$datang = array();
if($response){
foreach(json_decode($response)->data as $item){
$mediacount = $item->media_count;
$name = $item->name;
$datang[] = array(
"mediacount" => htmlspecialchars($mediacount),
"name" => htmlspecialchars($name),
);
}
}
print_r(str_replace('\/', '/', json_encode($datang)));
die();
输出没问题,目前.. 下一步,我想改变这个
$api = 'https://api.instagram.com/v1/tags/search?q='.$query.'&access_token='.$access_token.'';
进入这个
$api = "https://api.instagram.com/v1/tags/'.$query.'/media/recent?access_token=".$access_token."";
它 return 一个错误(从我的网络浏览器复制粘贴)
{
"pagination": {
"deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead"
},
"meta": {
"code": 200
},
"data": []
}
请社区,我该如何解决这个问题?有人吗? 我想像这里提到的那样输出(至少JSON)https://instagram.com/developer/endpoints/tags/
GET /tags/{tag-name}/media/recent..
感谢社区。
我觉得你的问题出在js
var sfield = $("#s");
var container = $("#photos");
您将 jquery 元素存储在变量中,稍后您在 jquery 元素上调用 jquery。将 $(sfield) 替换为 sfield。也为 container
这样做此问题已解决!