Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$search = 'ecig';
$url = 'https://api.instagram.com/v1/tags/' . $search .'/media/recent?client_id=YOUR_CLIENT_ID_HERE';
$json = file_get_contents($url);
$obj = json_decode($json);
date_default_timezone_set('EST');
$list = array();
foreach ($obj->data as $media) {
$username = $media->user->username;
$likes = $media->likes->count;
$comments = $media->comments->count;
$link = $media->link;
$caption = $media->caption->text;
$filter = $media->filter;
$hashcount = substr_count($caption, '#');
$hashtrue = (boolval($hashcount) ? 'true' : 'false');
$atcount = substr_count($caption, '@');
$attrue = (boolval($atcount) ? 'true' : 'false');
array_push($list, $username . 'ˇ' . $likes . 'ˇ' . $comments . 'ˇ' . $link . 'ˇ' . $caption . 'ˇ' . $filter . 'ˇ' . date(DATE_RFC2822) . 'ˇ' . $hashtrue . 'ˇ' . $hashcount . 'ˇ' . $attrue . 'ˇ' .$atcount) ;
}
$file = fopen($search .'data.csv',"a+");
foreach ($list as $line)
{
fputcsv($file,explode('ˇ',$line));
}
fclose($file); ?>
<?php
for ($x = 0; $x <= 10; $x++) {
$url = $url;
$next = $obj->pagination->next_url;
$url = $next;
$json = file_get_contents($url);
$obj = json_decode($json);
// echo $next . '<br>'; - was for testing url returns
$list = array();
foreach ($obj->data as $media) {
$username = $media->user->username;
$likes = $media->likes->count;
$comments = $media->comments->count;
$link = $media->link;
$caption = $media->caption->text;
$filter = $media->filter;
$hashcount = substr_count($caption, '#');
$hashtrue = (boolval($hashcount) ? 'true' : 'false');
$atcount = substr_count($caption, '@');
$attrue = (boolval($atcount) ? 'true' : 'false');
array_push($list, $username . 'ˇ' . $likes . 'ˇ' . $comments . 'ˇ' . $link . 'ˇ' . $caption . 'ˇ' . $filter . 'ˇ' . date(DATE_RFC2822) . 'ˇ' . $hashtrue . 'ˇ' . $hashcount . 'ˇ' . $attrue . 'ˇ' .$atcount) ;
}
$file = fopen($search .'data.csv',"a+");
foreach ($list as $line)
{
fputcsv($file,explode('ˇ',$line));
}
fclose($file);
};
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment