Scrape images from Instagram

This will likely break at some point. You should definitely store the result as a transient for performance.

function be_instagram_scrape( $user = false ) {

	if( ! $user )
		return;

	$insta_source = file_get_contents('http://instagram.com/' . esc_attr( $user ) );
	$shards = explode('window._sharedData = ', $insta_source);
	$insta_json = explode(';</script>', $shards[1]);
	$data = json_decode($insta_json[0], TRUE);

	$output = [];

	$images = $data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'];
	foreach( $images as $image_data ) {
		$image = $image_data['node'];

		$output[] = array(
                'description' => $image['edge_media_to_caption']['edges'][0]['node']['text'],
                'link'        => 'https://instagram.com/p/' . $image['shortcode'],
                'time'        => $image['taken_at_timestamp'],
                'comments'    => $image['edge_media_to_comment']['count'],
                'likes'       => $image['edge_media_preview_like']['count'],
                'thumbnail'   => $image['thumbnail_src'],
            );
	}

	return $output;
}
Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk