2

While calling getInsights() method,it gives an object.so i want to access some data from it. Here is the api call

$account->getInsights($fields, $params);
echo '<pre>';print_r($resultArr);die;

it will gives result like

FacebookAds\Cursor Object ( [response:protected] => FacebookAds\Http\Response Object ( [request:protected] => FacebookAds\Http\Request Object ( [client:protected] => FacebookAds\Http\Client Object ( [requestPrototype:protected] => FacebookAds\Http\Request Object (

Thanks in advance.

  • 1
    Cursors are for pagination; a cursor object itself should not contain any actual data AFAIK. – CBroe Mar 20 '17 at 13:57

4 Answers4

3

The following should work:

$resultArr = $account->getInsights($fields, $params)[0]->getData();
echo '<pre>';
print_r($resultArr);
die;

If you have more than one object in the cursor, you can just loop over it:

foreach ($account->getInsights($fields, $params) as $obj) {
    $resultArr = $obj->getData();
    echo '<pre>';
    print_r($resultArr);
}
die;

In this case, if you set the implicitFetch option to true by default with:

Cursor::setDefaultUseImplicitFetch(true);

you will be sure you are looping over all the results.

oriol
  • 31
  • 2
3

I using this piece of code and It works for me, I hope works for you ...

    $adset_insights = $ad_account->getInsights($fields,$params_c); 
    do {
            $adset_insights->fetchAfter();
    } while ($adset_insights->getNext());
    $adsets = $adset_insights->getArrayCopy(true); 
Angie
  • 255
  • 3
  • 7
2

Maybe try:

$insights = $account->getInsights($fields, $params);
$res = $insights->getResponse()->getContent(); 

and then go for the usual stuff:

print_r($res['data']);
ravb79
  • 722
  • 5
  • 8
0

Not sure if my method differs from Angelina's because it's a different area of the SDK or if it's because it's been changed since her answer, but below is the code that works for me, and hopefully will be useful for someone else:

        $location_objects = $cursor->getArrayCopy();
        $locations = array();

        foreach($location_objects as $loc)
        {
            $locations[] = $loc->getData();
        }
        return $locations;

Calling getArrayCopy returns an array of AbstractObjects and then calling getData returns an an array of the objects props.

Mark Hill
  • 161
  • 2
  • 9