Corey
Corey

Reputation: 2563

Instagram API: Get posts from a certain user that also has a certain hashtag

I know of these two endpoints: /users/{user-id}/media/recent and /tags/{tag-name}/media/recent

But I'm trying to get only posts by a certain user that also have a certain hashtag. Is there an easy way of doing this?

Currently I'm using the Instagram PHP API library, doing something like this:

require 'vendor/Instagram-PHP-API/instagram.class.php';
$api = new Instagram('API KEY');

// Get Hashtag Search
$result = $api->getTagMedia('somehashtag', 4); // This brings me back the last 4 posts by any user :/   

// Store in a local file for JS consumption
$json = json_encode($result);
$file = TEMPLATEPATH . '/js/instagrams.json';
$fh = fopen($file, 'w');
fwrite($fh, $json);
fclose($fh);

Anyone know of an easy way to do this?

Upvotes: 2

Views: 1310

Answers (1)

Corey
Corey

Reputation: 2563

This is what I ended up doing, just getting it down somewhere incase anyone else is trying to do the same thing.

require 'vendor/Instagram-PHP-API/instagram.class.php';
$api = new Instagram('API KEY'); // Client ID

// Get Recent Search
$result = $api->getUserMedia('THE USER ID', 20);
$data = $result->data;

$newresult = new stdClass();
$newdata = array();


foreach($data as $index=>$instagram) {
    if (in_array('somehashtag', $instagram->tags)) {
        array_push($newdata, $instagram);
    }
}

$newresult->data = $newdata;

$json = json_encode($newresult);
$file = TEMPLATEPATH . '/js/instagrams.json';
$fh = fopen($file, 'w');
fwrite($fh, $json);
fclose($fh);

So $newresult is my new object that only has posts from the specified user with the specified hashtag.

Upvotes: 5

Related Questions