Skip to content

IG User Media

Justin Stolpe edited this page Jan 3, 2023 · 18 revisions

Get and create media posts for a user.

Getting a Users Media

Get the users media.

YouTube Tutorial

use Instagram\User\Media;

$config = array( // instantiation config params
    'user_id' => '<IG_USER_ID>',
    'access_token' => '<ACCESS_TOKEN>',
);

// instantiate user media
$media = new Media( $config );

// initial user media response
$userMedia = $media->getSelf();

Creating an Image IG Container

Create an image IG Container for use in the post publishing process.

YouTube Tutorial

use Instagram\User\Media;

$config = array( // instantiation config params
    'user_id' => '<USER_ID>',
    'access_token' => '<ACCESS_TOKEN>',
);

// instantiate user media
$media = new Media( $config );

$imageContainerParams = array( // container parameters for the image post
    'caption' => '<CAPTION>', // caption for the post
    'image_url' => '<IMAGE_URL>', // url to the image must be on a public server
    'is_carousel_item' => <BOOLEAN>, // is this in a carousel
    'location_id' => '<LOCATION_ID>', // can be left blank otherwise the id of the facebook page associated with the location you want tagged
    'user_tags' => array( // array of users to tag in the image
        array( // key values for the tagged user
             'username' => '<USERNAME>', // user being tagged
             'x' => '<COORD_X>', // range 0.0 - 1.0 of where in the image the user gets tagged
             'y' => '<COORD_Y>' // range 0.0 - 1.0 of where in the image the user gets tagged
        )
    )
);

// create image container
$imageContainer = $media->create( $imageContainerParams );

// get id of the image container
$imageContainerId = $imageContainer['id'];

Creating a Video IG Container

Create an video IG Container for use in the post publishing process.

YouTube Tutorial

use Instagram\User\Media;

$config = array( // instantiation config params
    'user_id' => '<USER_ID>',
    'access_token' => '<ACCESS_TOKEN>',
);

// instantiate user media
$media = new Media( $config );

$videoContainerParams = array( // container parameters for the video post
    'caption' => '<CAPTION>', // caption for the post
    'video_url' => '<VIDEO_URL>', // url to the video must be on a public server
    'media_type' => '<MEDIA_TYPE>', // specifying this should be set to VIDEO
    'is_carousel_item' => <BOOLEAN>, // is this in a carousel
    'location_id' => '<LOCATION_ID>', // can be left blank otherwise the id of the facebook page associated with the location you want tagged
    'thumb_offset' => <THUMB_OFFSET> // number of milliseconds in the video to grab the thumbnail
);

// create video container
$videoContainer = $media->create( $videoContainerParams );

// get id of the video container
$videoContainerId = $videoContainer['id'];

Creating a Carousel IG Container

Create an carousel IG Container for use in the post publishing process.

YouTube Tutorial

use Instagram\User\Media;

$config = array( // instantiation config params
    'user_id' => '<USER_ID>',
    'access_token' => '<ACCESS_TOKEN>',
);

// instantiate user media
$media = new Media( $config );

$carouselContainerParams = array( // container parameters for the carousel post
    'caption' => '<CAPTION>', // caption for the post
    'children' => array( // array of container ids for the carousel up to 10 max
        '<CONATAINER_ID>',
        '<CONATAINER_ID>'
    ),
    'location_id' => '<LOCATION_ID>', // can be left blank otherwise the id of the facebook page associated with the location you want tagged
    'user_tags' => array( // array of users to tag in the image
        array( // key values for the tagged user
             'username' => '<USERNAME>', // user being tagged
             'x' => '<COORD_X>', // range 0.0 - 1.0 of where in the image the user gets tagged
             'y' => '<COORD_Y>' // range 0.0 - 1.0 of where in the image the user gets tagged
        )
    )
);

// create carousel container
$carouselContainer = $media->create( $carouselContainerParams );

// get id of the image container
$carouselContainerId = $carouselContainer['id'];

Pagination

The Instagram Graph API does not return all posts in one response but it will let us know if there are previous and next pages. The SDK takes care of building the links behind the scenes so all you need to do to get previous and next pages is make sure they exist and then call the getPage() function.

YouTube Tutorial

use Instagram\User\Media;
use Instagram\Request\Params;

$config = array( // instantiation config params
    'user_id' => '<IG_USER_ID>',
    'access_token' => '<ACCESS_TOKEN>',
);

// instantiate user media
$media = new Media( $config );

// initial user media response
$userMedia = $media->getSelf();

if ( $media->pagingNextLink ) { // get next page
    $nextPage = $media->getPage( Params::NEXT );
}

if ( $media->pagingPreviousLink ) { // get prev page
    $prevPage = $media->getPage( Params::PREV );
}