-
-
Save mxwright/54af2703f19ae6f83949969c7b0b752c to your computer and use it in GitHub Desktop.
Make a RSS feed from an Instagram user's username.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!isset($_GET['user'])) { | |
if (!isset($_GET['hashtag'])) { | |
exit('Not a valid RSS feed. You didn\'nt provide an Instagram user or hashtag. Send one via a GET variable. Example .../instarss.php?user=snoopdogg'); | |
} | |
} | |
if (isset($_GET['user']) && isset($_GET['hashtag'])) { | |
exit('Don\'t request both user and hashtag. Request one or the other.'); | |
} | |
if (isset($_GET['user'])) { | |
$html = file_get_contents('http://instagram.com/'.$_GET['user'].'/'); | |
} | |
if (isset($_GET['hashtag'])) { | |
$html = file_get_contents('http://instagram.com/explore/tags/'.$_GET['hashtag'].'/'); | |
} | |
$html = strstr($html, '"entry_data'); | |
$html = strstr($html, '</script>', true); | |
$html = substr($html, 0, -1); | |
$html = '{' . $html; | |
// for debugging... sigh........ | |
// echo $html; | |
$data = json_decode($html); | |
// more debugging... | |
// print_r($data->entry_data->ProfilePage[0]->user->media->nodes); | |
if (isset($_GET['user'])) { | |
if ($data->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges) { | |
$edges = $data->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges; | |
} else { | |
exit('Looks like this Instagram account is set to private or doesn\'t exist. We can\'t do much about that now, can we?'); | |
} | |
} | |
if (isset($_GET['hashtag'])) { | |
$edges = $data->entry_data->TagPage[0]->graphql->hashtag->edge_hashtag_to_media->edges; | |
} | |
header('Content-Type: text/xml; charset=utf-8'); | |
$rss_feed = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel>'; | |
if (isset($_GET['user'])) { | |
$rss_feed .= '<title>'.$_GET['user'].'\'s Instagram Feed</title><atom:link href="http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"].'" rel="self" type="application/rss+xml" /><link>http://instagram.com/'.$_GET['user'].'</link><description>'.$_GET['user'].'\'s Instagram Feed</description>'; | |
} | |
if (isset($_GET['hashtag'])) { | |
$rss_feed .= '<title>Photos tagged with: '.$_GET['hashtag'].' on Instagram</title><atom:link href="http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"].'" rel="self" type="application/rss+xml" /><link>http://instagram.com/explore/tags/'.$_GET['hashtag'].'</link><description>Photos tagged with: '.$_GET['hashtag'].' on Instagram</description>'; | |
} | |
foreach($edges as $nodes) { | |
$rss_feed .= '<item><title>'; | |
if(isset($nodes->node->edge_media_to_caption->edges[0]->node->text) && $nodes->node->edge_media_to_caption->edges[0]->node->text != '') { | |
$rss_feed .= htmlspecialchars($nodes->node->edge_media_to_caption->edges[0]->node->text, ENT_QUOTES); | |
} else { | |
$rss_feed .= 'photo'; | |
} | |
// pubdate format could also be: "D, d M Y H:i:s T" | |
$rss_feed .= '</title><link>https://instagram.com/p/'.$nodes->node->shortcode.'/</link><pubDate>'.date("r", $nodes->node->taken_at_timestamp).'</pubDate>'; | |
if (isset($_GET['user'])) { | |
$rss_feed .= '<dc:creator><![CDATA['.$_GET['user'].']]></dc:creator>'; | |
} | |
$rss_feed .= '<description><![CDATA[<img src="'.$nodes->node->display_url.'" />]]></description><guid>https://instagram.com/p/'.$nodes->node->shortcode.'/</guid></item>'; | |
} // foreach "node" (photo) | |
$rss_feed .= '</channel></rss>'; | |
echo $rss_feed; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
same for me. any suggestion? the file_get_contents() returns false