Question

I'm trying to filter out some unwanted Instagram images based on a review score that is a part of the photo caption.

I'm using the Instagram PHP API by cosenary for it and I have the data, and I have been able to filter out the entries with the correct syntax for a review score (*/10).

The problem is trying to get the objects back into json form so I can cache it so I dont have to keep making a million requests to Instagram.

I am trying to store the objects in an array and then encode the array back to json, but I keep getting the error in

 Trying to get property of non-object in /Users/***/Sites/instagram/index.php on line 16

Here is what I have so far.

<?php 

require_once('instagram.class.php');

$app_key = '<removed>';
$instagram = new Instagram($app_key);
$media = $instagram->getTagMedia('kittens');
$pattern = '/\d+\/+10/';
$fulldata = array();

if($media) {
    do {
        foreach ($media->data as $entry) {

            if(is_object($entry) && preg_match($pattern, $entry->caption->text)) {
                $fulldata[] = $entry;
            }
        }
    } while ($media = $instagram->pagination($media));
}

?>

<pre>
    <? echo json_encode($fulldata); ?>
</pre>

I'm not sure if that is_object($entry) does much.

Was it helpful?

Solution

This should work

<?php 

require_once('instagram.php');

$app_key = '';
$instagram = new Instagram($app_key);
$media = $instagram->getTagMedia('cats');
$pattern = '/\d+\/+10/';
$fulldata = array();
if($media) {
  do {
    foreach ($media->data as $entry) {
      $text = $entry->caption->text;

      if(preg_match($pattern, $text)) {
        $fulldata[] = $entry;
      }
    }
  } while ($media = $instagram->pagination($media));
}

?>

<pre>
    <? echo json_encode($fulldata); ?>
</pre>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top