简体   繁体   中英

how to access protected property response from facebook in facebook ads api

I am getting a protected object returned from the API call to Facebook Ads.

 $page = new Page($page_id);
 $leadgen_forms = $page->getLeadgenForms();

I am getting the response like this :

    FacebookAds\Object\Page
          (
      [response:protected] => FacebookAds\Http\Response Object
         (
            [response:protected] => Array
              (
                   [data] => Array
                        (
                             MY DATA
                        )
              )
    )
  )

I have already used used Reflection Class

 $reflect=new \ReflectionClass($leadgen_forms);
 $properties=$reflect->getProperties('content');

and PtcHandyMan

require_once('PtcHm.php');
 PtcHandyMan::register();
 $data = PtcHandyMan::getProperty( $leadgen_forms , 'content' );

but I am not able to access the protected property. Is there any way to access the protected property. And also I don't get why facebook is giving the protected response.

This is my whole function

  function get_page_forms(){
        if (!$this->input->is_ajax_request()) {
            exit('No direct script access allowed');
        }
        $page_id = $_POST['page_id'];
        $page_access_token = $_POST['page_access_token'];
        Api::init('APPID', 'SECRET KEY', $page_access_token);
        $page = new Page($page_id);
        $leadgen_forms = $page->getLeadgenForms();
        //$reflect=new \ReflectionClass($leadgen_forms);
        //$properties=$reflect->getProperties('content');
        require_once('PtcHm.php');
        PtcHandyMan::register();
        $data = PtcHandyMan::getProperty( $leadgen_forms , 'content' );
        $allFormsArr = $data['data'];           
        $count = count($allFormsArr);
        $output = '<div class="container">
                    <table class="table table-responsive table-hover">
                        <thead>
                            <tr>
                                <td>Id</td>
                                <td>Name</td>
                                <td>Leads csv file</td>
                                <td>Status</td>
                            </tr>
                        </thead>
                        <tbody>';
            for($i=0; $i<$count; $i++){
                $output .= '<tr>
                                <td>'.$allFormsArr[$i]['id'].'</td>
                                <td>'.$allFormsArr[$i]['name'].'</td>
                                <td><a href="'.$allFormsArr[$i]['leadgen_export_csv_url'].'" target="_new">Link</a></td>
                                <td>'.$allFormsArr[$i]['status'].'</td>
                            </tr>';
            }
           $output .= '</tbody>
                    </table>
                </div>';
        echo $output;
    }

Try using getter methods for the property like getContent . This might get you access to the protected property.

There are a lot ways to access the responses from facebook API.

Your code:

$page = new Page($page_id);
$leadgen_forms = $page->getLeadgenForms();

you are getting the response like this :

FacebookAds\Object\Page
  (
    [response:protected] => FacebookAds\Http\Response Object
       (
        [response:protected] => Array
          (
            [data] => Array
              (MY DATA)
          )
      )
  )

You can use asArray method

$page = new Page($page_id);
$leadgen_forms = $page->getLeadgenForms()->asArray();

https://developers.facebook.com/docs/php/GraphNode/5.0.0

or if you don't want to use asArray use simple foreach for deeper levels:

foreach ($leadgen_forms as $leadgen_form) {
  heres your array. It may be protected so you might want to dig deeper with another foreach and so on
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM