
Останні фото з інстаграма на PHP
Щоб вивести останні фото з інстаграма на PHP, необхідно написати 2 функції. У першій ми звернемося до API інстаграма:
// Get our data function fetchData( $url ){ $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_TIMEOUT, 20 ); $result = curl_exec( $ch ); curl_close( $ch ); return $result; } |
У другій – задамо кількість фотографій і виведемо на сторінку результат:
function instagram_feed( $instagram_token, $instagram_id ) { if ( $instagram_token != '' && $instagram_id != '' ){ // Pull and parse data. $result = fetchData( "https://api.instagram.com/v1/users/{$instagram_id}/media/recent/?access_token={$instagram_token}" ); $result = json_decode( $result ); $limit = 10; // Amount of images to show $i = 0; foreach ( $result->data as $post ){ if ( $i < $limit ){ echo '<a class="insta-wrap__link" rel="nofollow" target="_blank" href="' . $post->link . '">'; echo '<img class="insta-wrap__image" src="' . $post->images->thumbnail->url . '" alt="">'; echo '</a>'; $i ++; } } } } |
В $post->images->thumbnail->url можна замінити thumbnail на інші значення, щоб отримати фотографії з різним дозволом:
- thumbnail – 320*320px;
- low_resolution – 150*150px;
- standard_resolution – 640*640px;
І, нарешті, запускаємо функцію виведення там, де нам це потрібно:
<div class="instagram-feeds"> <?php $instagram_token = 'xxx'; $instagram_id = 'xxx'; if ( $instagram_token !='' && $instagram_id != '' ) { ?> <div class="insta-wrap"> <?php instagram_feed( $instagram_token, $instagram_id ); ?> </div> <?php } ?> </div> |
Змінна $instagram_token – це Instagram Access Token (як його отримати можна дізнатися, прочитавши статтю. $instagram_id – можна дізнатися, наприклад, тут. Просто замініть xxx на свої дані і ви отримаєте список своїх останніх фотографія з інстаграма.
Повний код:
<?php // Get our data function fetchData( $url ){ $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_TIMEOUT, 20 ); $result = curl_exec( $ch ); curl_close( $ch ); return $result; } function instagram_feed( $instagram_token, $instagram_id ) { if ( $instagram_token != '' && $instagram_id != '' ){ // Pull and parse data. $result = fetchData( "https://api.instagram.com/v1/users/{$instagram_id}/media/recent/?access_token={$instagram_token}" ); $result = json_decode( $result ); $limit = 10; // Amount of images to show $i = 0; foreach ( $result->data as $post ){ if ( $i < $limit ){ echo '<a class="insta-wrap__link" rel="nofollow" target="_blank" href="' . $post->link . '">'; echo '<img class="insta-wrap__image" src="' . $post->images->thumbnail->url . '" alt="">'; echo '</a>'; $i ++; } } } } ?> <div class="instagram-feeds"> <?php $instagram_token = 'xxx'; $instagram_id = 'xxx'; if ( $instagram_token !='' && $instagram_id != '' ) { ?> <div class="insta-wrap"> <?php instagram_feed( $instagram_token, $instagram_id ); ?> </div> <?php } ?> </div> |
>Підписуйтесь на наш Telegram канал, щоб не пропускати наші статті 😎
