Jquery 加载一个随机flickr图像&;追加到div
我基本上是尝试加载一张从特定用户和特定集合中随机拍摄的flickr图像,然后在id为“flickr wrap”的div中显示。我试图操纵这个JSON代码来做我想做的事情,但不知道从哪里开始。这段代码目前加载了很多图像(我只想加载一个),并使用了标记(但我想要用户和集合),有人能帮我吗Jquery 加载一个随机flickr图像&;追加到div,jquery,json,image,flickr,Jquery,Json,Image,Flickr,我基本上是尝试加载一张从特定用户和特定集合中随机拍摄的flickr图像,然后在id为“flickr wrap”的div中显示。我试图操纵这个JSON代码来做我想做的事情,但不知道从哪里开始。这段代码目前加载了很多图像(我只想加载一个),并使用了标记(但我想要用户和集合),有人能帮我吗 $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",{ id: "51997044@N03"
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",{
id: "51997044@N03",
tagmode: "any",
format: "json" },
function(data) {
$("<img/>").attr({src: data.items[0].media.m.replace('_m.','.')}).appendTo("#flickr-wrap");
});
$.getJSON(“http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",{
id:“51997044@N03",
tagmode:“任何”,
格式:“json”},
功能(数据){
$("
编辑
我已经停止了循环,这很好,现在更新了上面的代码,将photo set.gne而不是public.gne拉入,并通过删除一些代码行稍微改变了调用photoset的方式。现在我所需要做的就是从该集合中拉入一张随机图像。我现在得到的是:
$.getJSON("http://api.flickr.com/services/feeds/photoset.gne?set=72157626230243906&nsid=51997044@N03&lang=en-us&format=json&jsoncallback=?",
function(data) {
$("<img/>").attr({src: data.items[0].media.m.replace('_m.','.')}).appendTo("#flickr-wrap");
});
$.getJSON(“http://api.flickr.com/services/feeds/photoset.gne?set=72157626230243906&nsid=51997044@N03&lang=en us&format=json&jsoncallback=?“,
功能(数据){
$("
编辑
仍然没有得到随机的东西来工作。最烦人的是。可能真的需要一些帮助。绝望!数据。items
是图像的数组,所以只需获取第一个,不要在数组上迭代
而不是
$.each(data.items, function(i,item){...}
做
$(“您可能需要获取更大的图像,因为您需要API密钥。然后,您可以调用此函数,即:
function getPicture(the_user_id, your_div_id){
var apiKey = "YOUR-API-KEY"; // replace this with your API key
// get an array of random photos
$.getJSON(
"http://api.flickr.com/services/rest/",
{
method: 'flickr.people.getPublicPhotos',
api_key: apiKey,
user_id: the_user_id,
format: 'json',
nojsoncallback: 1,
per_page: 10 // you can increase this to get a bigger array
},
function(data){
// if everything went good
if(data.stat == 'ok'){
// get a random id from the array
var photoId = data.photos.photo[ Math.floor( Math.random() * data.photos.photo.length ) ];
// now call the flickr API and get the picture with a nice size
$.getJSON(
"http://api.flickr.com/services/rest/",
{
method: 'flickr.photos.getSizes',
api_key: apiKey,
photo_id: photoId,
format: 'json',
nojsoncallback: 1
},
function(response){
if(response.stat == 'ok'){
var the_url = response.sizes.size[5].source;
$("#"+your_div_id).append('<img src="' + the_url + '" />');
}
else{
console.log(" The request to get the picture was not good :\ ")
}
}
);
}
else{
console.log(" The request to get the array was not good :( ");
}
}
);
};
函数getPicture(用户id、您的分区id){
var apiKey=“YOUR-API-KEY”;//将其替换为您的API密钥
//获取一组随机照片
$.getJSON(
"http://api.flickr.com/services/rest/",
{
方法:“flickr.people.getPublicPhotos”,
api_密钥:api密钥,
用户标识:用户标识,
格式:“json”,
nojsoncallback:1,
每页:10//您可以增加此值以获得更大的数组
},
功能(数据){
//如果一切顺利
如果(data.stat=='ok'){
//从数组中获取随机id
var photoId=data.photos.photo[Math.floor(Math.random()*data.photos.photo.length)];
//现在调用flickr API并获得大小合适的图片
$.getJSON(
"http://api.flickr.com/services/rest/",
{
方法:“flickr.photos.getSizes”,
api_密钥:api密钥,
照片id:photoId,
格式:“json”,
nojsoncallback:1
},
功能(响应){
如果(response.stat=='ok'){
var the_url=response.size.size[5].source;
$(“#”+您的_div_id);
}
否则{
log(“获取图片的请求不好:\”)
}
}
);
}
否则{
log(“获取阵列的请求不好:(”);
}
}
);
};
我注意到上面的答案中有几个拼写错误。下面是一个代码示例,其中拼写错误已更正,还有一些小改动
function getPicture(the_user_id, your_div_id){
var apiKey = "YOUR-API-KEY"; // replace this with your API key
// get an array of random photos
$.getJSON(
"http://api.flickr.com/services/rest/",
{
method: 'flickr.interestingness.getList',
api_key: apiKey,
format: 'json',
nojsoncallback: 1,
per_page: 10 // you can increase this to get a bigger array
},
function(data){
// if everything went good
if(data.stat == 'ok'){
// get a random id from the array
var photo = data.photos.photo[ Math.floor( Math.random() * data.photos.photo.length ) ];
// now call the flickr API and get the picture with a nice size
$.getJSON(
"http://api.flickr.com/services/rest/",
{
method: 'flickr.photos.getSizes',
api_key: apiKey,
photo_id: photo.id,
format: 'json',
nojsoncallback: 1
},
function(response){
if(response.stat == 'ok'){
var the_url = response.sizes.size[5].source;
$("#"+your_div_id).append("");
}
else{
console.log(" The request to get the picture was not good :\ ")
}
}
);
}
else{
console.log(" The request to get the array was not good :( ");
}
}
);
};
函数getPicture(用户id、您的分区id){
var apiKey=“YOUR-API-KEY”;//将其替换为您的API密钥
//获取一组随机照片
$.getJSON(
"http://api.flickr.com/services/rest/",
{
方法:“flickr.interestingness.getList”,
api_密钥:api密钥,
格式:“json”,
nojsoncallback:1,
每页:10//您可以增加此值以获得更大的数组
},
功能(数据){
//如果一切顺利
如果(data.stat=='ok'){
//从数组中获取随机id
var photo=data.photos.photo[Math.floor(Math.random()*data.photos.photo.length)];
//现在调用flickr API并获得大小合适的图片
$.getJSON(
"http://api.flickr.com/services/rest/",
{
方法:“flickr.photos.getSizes”,
api_密钥:api密钥,
photo_id:photo.id,
格式:“json”,
nojsoncallback:1
},
功能(响应){
如果(response.stat=='ok'){
var the_url=response.size.size[5].source;
$(“#”+您的_div_id);
}
否则{
log(“获取图片的请求不好:\”)
}
}
);
}
否则{
log(“获取阵列的请求不好:(”);
}
}
);
};
最后,我不得不采取一种完全不同的方法。事实证明,在我试图找出这个问题的答案时,Flickr badge API发生了变化,增加了更多的灵活性。它基本上完成了我现在需要做的事情:这里是一个更新的fiddle+示例,允许您通过标记获取随机图像(我需要这个,觉得可能会有帮助)
完整示例:
function getPicture(tags, cb) {
var apiKey = "fa214b1215cd1a537018cfbdfa7fb9a6"; // replace this with your API key
// get an array of random photos
$.getJSON(
"https://api.flickr.com/services/rest/?jsoncallback=?", {
method: 'flickr.photos.search',
tags: tags,
api_key: apiKey,
format: 'json',
per_page: 10 // you can increase this to get a bigger array
},
function(data) {
// if everything went good
if (data.stat == 'ok') {
// get a random id from the array
var photo = data.photos.photo[Math.floor(Math.random() * data.photos.photo.length)];
// now call the flickr API and get the picture with a nice size
$.getJSON(
"https://api.flickr.com/services/rest/?jsoncallback=?", {
method: 'flickr.photos.getSizes',
api_key: apiKey,
photo_id: photo.id,
format: 'json'
},
function(response) {
if (response.stat == 'ok') {
var the_url = response.sizes.size[5].source;
cb(the_url);
} else {
console.log(" The request to get the picture was not good :\ ")
}
}
);
} else {
console.log(" The request to get the array was not good :( ");
}
}
);
};
按如下方式调用代码:
getPicture('<your_tag>', function(url) {
$("#random").attr("src", url)
});
getPicture(“”,函数(url){
$(“#random”).attr(“src”,url)
});
太好了,谢谢!您还知道我如何定义flickr用户和照片集吗
getPicture('<your_tag>', function(url) {
$("#random").attr("src", url)
});