Last active
August 29, 2015 13:56
-
-
Save distresslife/9205608 to your computer and use it in GitHub Desktop.
串接 instagram api,給 user_name 後 download 所有該 user 的 public images
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 | |
function get_instagram_uid($user_name,$access_token){ | |
$q_array = array( | |
'q' => strtolower($user_name), | |
'access_token' => $access_token, | |
); | |
$url = 'https://api.instagram.com/v1/users/search?' . http_build_query($q_array); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_SSLVERSION, 3); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$json = curl_exec($ch); | |
curl_close($ch); | |
$json = json_decode($json); | |
foreach( $json->data as $k => $row){ | |
if( $row->username == $user_name){ | |
return $row->id; | |
} | |
} | |
return false; | |
} | |
function download_user_media($user_id,$access_token,$save_dir){ | |
$d_array = array( | |
'count' => '-1', | |
'access_token' => $access_token, | |
); | |
$url = 'https://api.instagram.com/v1/users/' . $user_id . '/media/recent/?' . http_build_query($d_array); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_SSLVERSION, 3); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
$output = json_decode($output); | |
foreach( $output->data as $k => $row){ | |
$image_url = $row->images->standard_resolution->url; | |
$url_array = pathinfo($image_url); | |
$new_file = $save_dir . '/' . $url_array['basename']; | |
if( !file_exists($new_file) ){ | |
$s = file_put_contents($new_file,file_get_contents($image_url)); | |
if(!$s){ | |
die('copy image error.'); | |
} | |
} | |
} | |
} | |
ini_set('max_execution_time', 0); | |
$access_token = '510573486.ab7d4b6.d8b155be5d1a47c78f72616b4d942e8d'; | |
$username_array = array( | |
'smooshblog', | |
); | |
$start = microtime(true); | |
if (!file_exists('./instagram_download') and !is_dir('./instagram_download')) { | |
mkdir('./instagram_download'); | |
} | |
foreach( $username_array as $user_name ){ | |
$user_id = get_instagram_uid($user_name,$access_token); | |
$save_dir = './instagram_download/' . $user_name; | |
if (!file_exists($save_dir) and !is_dir($save_dir)) { | |
mkdir($save_dir); | |
} | |
download_user_media($user_id,$access_token,$save_dir); | |
echo $user_name . ' download had success.<br />'; | |
} | |
$end = microtime(true); | |
$time = number_format(($end - $start), 2); | |
echo 'Download spent ', $time, ' seconds'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment