将Instagram照片显示在我的网页上

时间:2014-07-03 14:26:40

标签: jquery ajax wordpress instagram

我正在寻找在我的网页上显示不是我的Instagram照片的方法,但不能。 如果我只有instagram用户的帐户名(例如jamieoliver),是否可以这样做。 我的网站是用Wordpress写的。

需要显示不是我的图片

1 个答案:

答案 0 :(得分:8)

此网址格式http://instagram.com/{instagram user name}/media将返回包含该用户最新(20 +/-)媒体文件的json文件。

jamieoliver的示例中,您可以执行http://instagram.com/jamieoliver/media

您可以通过(jQuery)ajax调用处理json响应,如:

$.ajax({
    url: "http://instagram.com/jamieoliver/media",
    dataType : "jsonp", // this is important
    cache: false,
    success: function(response){
        // process the json response to get images
        // e.g. the first image should be something like : 
        // response.items.images[0].low_resolution
        // you could call an external function to iterate through the response
    }
});

当然,我假设您了解json格式的样子。如果您使用的是WordPress,也许您可​​以找到一个插件来处理该json响应


修改

http://instagram.com/{author_name}/media的响应似乎不是jsonp而是json(请参阅this以获取进一步参考),但设置json dataType将返回跨域错误。

解决方法是使用whateverorigin.org第三方应用来规避同源策略。

格式化您的网址,如

"http://whateverorigin.org/get?url=" + encodeURIComponent("http://instagram.com/{author_name}/media");

whateverorigin服务器将充当代理并返回正确的json格式。

注意您仍需要在ajax调用中使用dataType : "jsonp"

参见 JSFIDDLE