Source: instagram2rss.phps - download
<?php
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* instagram2rss.phps 0.3
* by daif alotaibi (http://daif.net)
* daif@daif.net
*
* Link:
* http://daif.net/script/instagram2rss.phps
*
* Examples:
* instagram2rss.php?path=explore/tags/php
* instagram2rss.php?path=explore/locations/213008247
* instagram2rss.php?path=daifnet
*
*/
if(!isset($_GET['path'])) {
print 'Error: please set "path" parameter';
exit;
}
$url = 'https://www.instagram.com/'.$_GET['path'];
$data = file_get_contents($url);
$rss = array();
$items = array();
preg_match('#_sharedData\s*=\s*(.+)\s*;<#Uis', $data, $_sharedData);
if(isset($_sharedData[1])) {
$_sharedData = json_decode($_sharedData[1]);
}
// if the link is a hashtag
if(isset($_sharedData->entry_data->TagPage[0])) {
$nodes = $_sharedData->entry_data->TagPage[0]->graphql->hashtag->edge_hashtag_to_media->edges;
$rss['title'] = 'Instagram #'.$_sharedData->entry_data->TagPage[0]->tag->name;
}
// if the link is a user
if(isset($_sharedData->entry_data->ProfilePage[0])) {
$nodes = $_sharedData->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges;
$rss['title'] = 'Instagram @'.$_sharedData->entry_data->ProfilePage[0]->graphql->user->username;
}
// if the link is a location
if(isset($_sharedData->entry_data->LocationsPage[0])) {
$nodes = $_sharedData->entry_data->LocationsPage[0]->graphql->location->edge_location_to_media->edges;
$rss['title'] = 'Instagram in: '.$_sharedData->entry_data->LocationsPage[0]->graphql->location->name;
}
// build items array
if(isset($nodes)) {
foreach ($nodes as $key => $item) {
$caption = $item->node->edge_media_to_caption->edges[0]->node->text;
$items[$key]['title'] = StripBadChars(substr($caption, 0, 32));
$items[$key]['link'] = 'https://www.instagram.com/p/'.$item->node->shortcode.'/';
$items[$key]['text'] = StripBadChars($caption);
$items[$key]['date'] = date('Y-m-d H:i:s', $item->node->taken_at_timestamp);
$items[$key]['image'] = $item->thumbnail_src;
}
}
// remove some invalid chars
function StripBadChars($string) {
// Strip invalid UTF-8 byte sequences - this part may not be strictly necessary, could be separated to another function
$string = mb_convert_encoding(mb_convert_encoding($string, 'UTF-16', 'UTF-8'), 'UTF-8', 'UTF-16');
// Remove various characters not allowed in XML
$string = preg_replace('/[^\x{0009}\x{000A}\x{000D}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]/u', '�', $string);
return $string;
}
// Generate xml
$xml = array();
$xml[] = '<?xml version="1.0" encoding="utf-8"?>';
$xml[] = '<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">';
$xml[] = ' <channel>';
$xml[] = ' <title><![CDATA['.$rss['title'].']]></title>';
$xml[] = ' <link>'.$url.'</link>';
$xml[] = ' <description><![CDATA['.$rss['title'].']]></description>';
$xml[] = ' <pubDate>'.date('D, d M Y H:i:s', strtotime($items[0]['date'])).' +0000</pubDate>';
$xml[] = ' <lastBuildDate>'.date('D, d M Y H:i:s', strtotime($items[0]['date'])).' +0000</lastBuildDate>';
$xml[] = ' <language>en</language>';
$xml[] = ' <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="'.$rss['link'].'" />';
foreach ($items as $key => $item) {
$xml[] = ' <item>';
$xml[] = ' <title><![CDATA['.$item['title'].']]></title>';
$xml[] = ' <link>'.$item['link'].'</link>';
$xml[] = ' <guid isPermaLink="false">'.$item['link'].'</guid>';
$xml[] = ' <pubDate>'.date('D, d M Y H:i:s', strtotime($item['date'])).' +0000</pubDate>';
$xml[] = ' <description><![CDATA[ <img src="'.$item['image'].'"><br />'.$item['text'].']]></description>';
$xml[] = ' </item>';
}
$xml[] = ' </channel>';
$xml[] = '</rss>';
// print xml
header('Content-Type: text/xml');
print implode("\n", $xml);
?>