Last active
August 5, 2016 09:37
-
-
Save azharisubroto/549c018ff3a2eaba967ef3a06fbbfff0 to your computer and use it in GitHub Desktop.
easy instagram scraper.
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 | |
$instagramusername = 'azhari.subroto'; | |
$args = array( | |
'timeout' => 360, | |
'httpversion' => '1.1' | |
); | |
$response = wp_remote_get('http://instagram.com/'.$instagramusername, $args); | |
// Check for error | |
if ( is_wp_error( $response ) ) { | |
return; | |
} | |
if( is_array( $response ) ){ | |
$insta_source = wp_remote_retrieve_body( $response ); | |
// Check for error | |
if ( is_wp_error( $insta_source ) ) { | |
return; | |
} | |
$shards = explode('window._sharedData = ', $insta_source); | |
$insta_json = explode(';</script>', $shards[1]); | |
$results_array = json_decode($insta_json[0], TRUE); | |
// echo '<pre>'; | |
// print_r($results_array); | |
// echo '</pre>'; | |
if( is_array( $results_array ) && !empty( $results_array ) ){ | |
//An example of where to go from there | |
$latest_array = $results_array['entry_data']['ProfilePage'][0]['user']['media']['nodes']; | |
if( is_array( $latest_array ) && !empty( $latest_array ) ){ | |
echo '<div class="zl_instagram_photo_wrapper">'; | |
$i=1; | |
foreach( $latest_array as $photo ){ | |
echo '<a href="http://instagram.com/p/'.$photo['code'].'"><img src="'.$photo['thumbnail_src'].'" alt=""/></a>'; | |
// break when the 6th photo fetched | |
if( 6 == $i ) break; | |
$i++; | |
} | |
echo '</div>'; | |
} | |
} else { | |
echo '<code>'; | |
esc_html_e('An Error Occured. Check your connection or make sure the username is exists', 'zeytin'); | |
echo '</code>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment