這樣做的目標是自動添加歸因鏈接到博客文章中使用的圖像。我有一個demo set up here手動使用flickr.photos.getInfo
在圖像上構建歸因網址。從靜態圖像URL中提取Flickr照片ID
爲此,我從CSS中的background-image
拍攝了照片ID並創建了API調用。我想要自動從background-image
網址中拉取照片ID(在此示例中爲),以在每篇文章中創建API調用。
CSS
.featured {
background-image:url('https://farm3.staticflickr.com/2613/3990985751_7ca0769f15_b.jpg');
}
HTML
<div class="featured">
<body>
<div class="featured">
<div id="featured-credit">
<p id="credits"></p>
</div>
</div>
</div>
的jQuery/JS
// Builds the URL to link in the image credit
function jsonFlickrApi (response) {
$("#credits").html('<a href="http://www.flickr.com/photos/'+response.photo.owner.nsid+'/'+response.photo.id+'/" target="blank">'+response.photo.title._content+"</a>");
}
// removes the CSS formatting for the featured image background URL
function extractUrl(input) {
return input.replace(/"/g,"").replace(/url\(|\)$/ig, "");
}
/* After all the scripts are loaded, send the featured photo to the Flickr API to get the JSON data */
<script src="https://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=APIKEYHERE&photo_id=3990985751&format=json&jsoncallback=?"></script>
我研究了等一幫和其他網站和我發現的其他解決方案都使用配置文件URL,而我需要使用靜態源URL。任何幫助表示讚賞。
我無法得到這個工作,並且無法弄清楚爲什麼'success'代碼沒有被觸發,即使API調用成功。我最終使用了'$ .getJSON'方法。 – Brian 2014-09-23 20:41:49
對不起,它缺少參數contentType:「application/json; charset = utf-8」。 – 2014-09-23 20:53:31
不用擔心...你仍然指出我正確的軌道:-) – Brian 2014-09-23 20:59:55