我有一个非常简单的脚本,它可以帮助我在我的网站上显示IG图像并且效果很好,但理想情况下我希望一旦点击图像就会链接到实际的帖子而不是图像源。有什么方法可以调整这个吗?
PS:我知道如何摆脱链接中的fancybox - 但我不知道如何以及从何处检索实际的帖子ID /链接。
这是我目前的代码:
<?php
function rudr_instagram_api_curl_connect( $api_url ){
$connection_c = curl_init(); // initializing
curl_setopt( $connection_c, CURLOPT_URL, $api_url ); // API URL to connect
curl_setopt( $connection_c, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
curl_setopt( $connection_c, CURLOPT_TIMEOUT, 20 );
$json_return = curl_exec( $connection_c ); // connect and get json data
curl_close( $connection_c ); // close connection
return json_decode( $json_return ); // decode and return
}
$access_token = 'my access token ';
$username = 'my username';
$user_search = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/users/search?q=" . $username . "&access_token=" . $access_token);
// $user_search is an array of objects of all found users
// we need only the object of the most relevant user - $user_search->data[0]
// $user_search->data[0]->id - User ID
// $user_search->data[0]->first_name - User First name
// $user_search->data[0]->last_name - User Last name
// $user_search->data[0]->profile_picture - User Profile Picture URL
$limit = 8;
$i = 0;
// $user_search->data[0]->username - Username
$user_id = $user_search->data[0]->id; // or use string 'self' to get your own media
$return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/users/" . $user_id . "/media/recent?access_token=" . $access_token);
//var_dump( $return ); // if you want to display everything the function returns
foreach ($return->data as $post) {
echo '<a href="' . $post->images->standard_resolution->url . '" class="fancybox"><img src="' . $post->images->standard_resolution->url . '" /></a>';
}
?>
答案 0 :(得分:1)
实际上你正在链接到$post->images->standard_resolution->url
现在,如果我们看一下API返回的内容(https://www.instagram.com/developer/endpoints/users/)
},
"link": "http://instagr.am/p/BWrVZ/",
"user": {
"username": "kevin",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_3_75sq_1295574122.jpg",
"id": "3"
},
"created_time": "1296710327",
"images": {
"low_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_7.jpg",
"width": 612,
"height": 612
}
正如您所看到的,在顶部有link
- 您所要做的就是将您的href更改为此链接而不是图像网址。
所以改变:
echo '<a href="' . $post->images->standard_resolution->url . '" class="fancybox"><img src="' . $post->images->standard_resolution->url . '" /></a>';
为:
echo '<a href="' . $post->link . '" class="fancybox"><img src="' . $post->images->standard_resolution->url . '" /></a>';
它应该有效。希望它有所帮助。