Skip to content

Instantly share code, notes, and snippets.

@nczz
Forked from gsarig/get_instagram_media.php
Last active September 10, 2020 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nczz/ea96026c2c2b9fabb7b1140516a8c54a to your computer and use it in GitHub Desktop.
Save nczz/ea96026c2c2b9fabb7b1140516a8c54a to your computer and use it in GitHub Desktop.
[WordPress] 使用 Instagram API 嵌入圖文至網站的正確做法(2020/07後適用) https://www.mxp.tw/9037/
<?php
/**
* 使用 Instagram (Facebook) API 取得用戶圖文的方法,翻譯自原文: https://www.gsarigiannidis.gr/instagram-feed-api-after-june-2020/
*
* @param $token // 存取權杖
* @param $user // 用戶編號,查詢工具: https://developers.facebook.com/tools/debug/accesstoken/
* @param int $limit // 查詢圖文筆數(不建議設定太多).
* @param string $fields // 其他欄位參考: https://developers.facebook.com/docs/instagram-basic-display-api/reference/media
* @param array $restrict // 取得媒體類型: IMAGE, VIDEO, CAROUSEL_ALBUM
*
* @return array|mixed // 使用方式: get_instagram_media(存取權杖, 用戶編號);
*/
function get_instagram_media(
$token,
$user,
$limit = 10,
$fields = 'media_url,permalink,media_type,caption',
$restrict = [ 'IMAGE' ]
) {
// The request URL. see: https://developers.facebook.com/docs/instagram-basic-display-api/reference/user
$request_url = 'https://graph.instagram.com/' . $user . '?fields=media&access_token=' . $token;
// 使用 WordPress Transient API 快取功能一小時更新一次,來避免觸發 Instagram API 存取速率限制 (see: https://developers.facebook.com/docs/graph-api/overview/rate-limiting#instagram-graph-api)
$output = get_transient( 'instagram_feed_' . $user ); // 使用用戶編號組合成特殊不重複字串來建立快取
if ( false === ( $data = $output ) || empty( $output ) ) {
// Prepare the data variable and set it as an empty array.
$data = [];
// Make the request
$response = wp_safe_remote_get( $request_url );
$response_body = '';
if ( is_array( $response ) && ! is_wp_error( $response ) ) {
$response_body = json_decode( $response['body'] );
}
if ( $response_body && isset( $response_body->media->data ) ) {
$i = 0;
// Get each media item from it's ID and push it to the $data array.
foreach ( $response_body->media->data as $media ) {
if ( $limit > $i ) {
$request_media_url = 'https://graph.instagram.com/' . $media->id . '?fields=' . $fields . '&access_token=' . $token;
$media_response = wp_safe_remote_get( $request_media_url );
if ( is_array( $media_response ) && ! is_wp_error( $media_response ) ) {
$media_body = json_decode( $media_response['body'] );
}
if ( in_array( $media_body->media_type, $restrict, true ) ) {
$i ++;
$data[] = $media_body;
}
}
}
}
// Store the data in the transient and keep if for an hour.
set_transient( 'instagram_feed_' . $user, $data, HOUR_IN_SECONDS );
// 每次存取都去更新權杖,避免過期不能使用 (see: https://developers.facebook.com/docs/instagram-basic-display-api/guides/long-lived-access-tokens#refresh-a-long-lived-token)
wp_safe_remote_get( 'https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=' . $token );
$output = $data;
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment