Last active
August 12, 2017 03:35
-
-
Save daiki44/6e575ca99b8be94e681853fa5ed39af8 to your computer and use it in GitHub Desktop.
LaravelでInstagramに投稿された写真を全取得してみた ref: http://qiita.com/daiki_44/items/9a1f3f78a76dd3dc8e1f
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
/** | |
* @params string $instagram_id OAuth認証後、取得されたInstagramID | |
* @params string $access_token OAuth認証後、取得されたaccess_token | |
*/ | |
public function getMediaList($instagram_id, $access_token) { | |
# 1回目のloopを回すため20に | |
$count = 20; | |
$max_id = null; | |
$data = array(); | |
$status = true; | |
while ($count == 20) { | |
$media_api_url = 'https://api.instagram.com/v1/users/' . $instagram_id . '/media/recent?access_token=' . $access_token. '&count=20'; | |
# max_idがセットされていたら、パラメータに付与 | |
# ここでページングのような感じになる | |
if (!is_null($max_id)) { | |
$media_api_url .= '&max_id=' . $max_id; | |
} | |
$context = stream_context_create(array('http' => array('ignore_errors' => true))); | |
$json = file_get_contents($media_api_url, false, $context); | |
$status_code = $this->getStatusCode($http_response_header[0]); | |
if ($status_code == 200) { | |
# decodeした配列をpush | |
$media_list = json_decode($json); | |
array_push($data, $media_list->data); | |
# 20投稿取れたらまだ次ページがあるのでループを続ける | |
$count = count($media_list->data); | |
if ($count == 20) { | |
$max_id = $media_list->data[$count - 1]->id; | |
} | |
} else { | |
# 1度でもstatus_codeが200以外だったら、何もしないのでfalseで返す | |
$status = false; | |
break; | |
} | |
} | |
if ($status) { | |
return $data; | |
} | |
return false; | |
} | |
/** | |
* file_get_contets後、status_codeを取得する | |
*/ | |
public function getStatusCode($http_response_header) { | |
preg_match('/HTTP\/1\.[0|1|x] ([0-9]{3})/', $http_response_header, $matches); | |
$status_code = $matches[1]; | |
return $status_code; | |
} |
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
array(1) { | |
# ページ番号 | |
[0]=> | |
# mediaの配列 | |
array(2) { | |
# 1つ目のmedia | |
[0]=> | |
object(stdClass)#159 (15) { | |
# media id | |
["id"]=> | |
string(28) "283029249630288979_223689238" | |
["user"]=> | |
object(stdClass)#160 (4) { | |
["id"]=> | |
string(9) "223689238" | |
["full_name"]=> | |
string(15) "Daiki Sekiguchi" | |
["profile_picture"]=> | |
string(105) "https://scontent.cdninstagram.com/t51.2885-19/s150x150/16789727_236142133514690_5385687353354354688_a.jpg" | |
["username"]=> | |
string(15) "daiki.sekiguchi" | |
} | |
# 投稿写真。サムネとか低画質とか色々な種類があるので、必要なものを適宜使用。 | |
["images"]=> | |
object(stdClass)#161 (3) { | |
["thumbnail"]=> | |
object(stdClass)#162 (3) { | |
["width"]=> | |
int(150) | |
["height"]=> | |
int(150) | |
["url"]=> | |
string(101) "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/11230350_1098596466824017_1845423183_n.jpg" | |
} | |
["low_resolution"]=> | |
object(stdClass)#163 (3) { | |
["width"]=> | |
int(320) | |
["height"]=> | |
int(320) | |
["url"]=> | |
string(101) "https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/11230350_1098596466824017_1845423183_n.jpg" | |
} | |
["standard_resolution"]=> | |
object(stdClass)#164 (3) { | |
["width"]=> | |
int(612) | |
["height"]=> | |
int(612) | |
["url"]=> | |
string(92) "https://scontent.cdninstagram.com/t51.2885-15/e15/11230350_1098596466824017_1845423183_n.jpg" | |
} | |
} | |
["created_time"]=> | |
string(10) "1347959737" | |
["caption"]=> | |
NULL | |
["user_has_liked"]=> | |
bool(false) | |
["likes"]=> | |
object(stdClass)#165 (1) { | |
["count"]=> | |
int(5) | |
} | |
["tags"]=> | |
array(0) { | |
} | |
["filter"]=> | |
string(5) "Amaro" | |
["comments"]=> | |
object(stdClass)#166 (1) { | |
["count"]=> | |
int(0) | |
} | |
["type"]=> | |
string(5) "image" | |
["link"]=> | |
string(39) "https://www.instagram.com/p/PthZpdQjxT/" | |
["location"]=> | |
NULL | |
["attribution"]=> | |
NULL | |
["users_in_photo"]=> | |
array(0) { | |
} | |
} | |
# 2件目の投稿 | |
[1]=> | |
object(stdClass)#167 (15) { | |
["id"]=> | |
string(28) "282904296255798780_223689238" | |
続く... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment