skip to Main Content

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

FacebookAdsCursor Object
(
[response:protected] => FacebookAdsHttpResponse Object
(
[request:protected] => FacebookAdsHttpRequest Object
(
[client:protected] => FacebookAdsHttpClient Object
(
[requestPrototype:protected] => FacebookAdsHttpRequest Object
(

Thanks in advance.

4

Answers


  1. 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.

    Login or Signup to reply.
  2. 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); 
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. Maybe try:

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

    and then go for the usual stuff:

    print_r($res['data']);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search