예제 #1
0
/**
 * Retrieves Instagram photos for a given username.
 * 
 * @param string $username An Instagram handle without @
 * @param int $num How many photos to return. Defaults to 10.
 * @param string $tag A hashtag to filter the query
 * @return array Returns an array of photos with keys time, img, and url.
 */
function getInstagram($username, $num = 10, $tag = false)
{
    if (INSTAGRAM_APP_KEY) {
        $instagram = new Instagram(INSTAGRAM_APP_KEY);
    } else {
        throw new \RuntimeException('Instagram API Credentials have not been defined.');
    }
    $user_id = getInstaUserId($username, $instagram);
    $media_obj = $instagram->getUserMedia($user_id, 33);
    // Instagram limit of 33
    $loop_count = 0;
    do {
        if (!isset($photos)) {
            $photos = buildInstaArray($media_obj, $tag);
        } else {
            $media_obj = $instagram->pagination($media_obj);
            $more_photos = buildInstaArray($media_obj, $tag);
            $photos = array_merge($photos, $more_photos);
        }
        $photos_returned = count($photos);
        $loop_count++;
    } while ($photos_returned < $num && $loop_count <= 7);
    return array_slice($photos, 0, $num);
}
예제 #2
0
/**
 * Retrieves Instagram photos for a given username.
 * 
 * @param string $username An Instagram handle without @
 * @param int $num How many photos to return. Defaults to 10.
 * @param string $tag A hashtag to filter the query
 * @return array Returns an array of photos with keys time, img, and url.
 */
function getInstagram($username, $num = 10, $tag = false)
{
    if (get_option('instagram_app_key')) {
        $instagram = new Instagram(get_option('instagram_app_key'));
    } else {
        $instagram = new Instagram(INSTAGRAM_APP_KEY);
    }
    $user_id = getInstaUserId($username, $instagram);
    $media_obj = $instagram->getUserMedia($user_id, 33);
    // Instagram limit of 33
    $loop_count = 0;
    do {
        if (!isset($photos)) {
            $photos = buildInstaArray($media_obj, $tag);
        } else {
            $media_obj = $instagram->pagination($media_obj);
            $more_photos = buildInstaArray($media_obj, $tag);
            $photos = array_merge($photos, $more_photos);
        }
        $photos_returned = count($photos);
        $loop_count++;
    } while ($photos_returned < $num && $loop_count <= 7);
    return array_slice($photos, 0, $num);
}