facebook / facebook-php-business-sdk

PHP SDK for Meta Marketing API
https://developers.facebook.com/docs/business-sdk
Other
818 stars 514 forks source link

Call to undefined method FacebookAds\Object\Lead::getContent() #478

Closed 8lall0 closed 5 years ago

8lall0 commented 5 years ago

Using the example for leadgen:

require __DIR__ . '/vendor/autoload.php';

use FacebookAds\Object\Lead;
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;

$access_token = '<ACCESS_TOKEN>';
$app_secret = '<APP_SECRET>';
$app_id = '<APP_ID>';
$id = '<ID>';

$api = Api::init($app_id, $app_secret, $access_token);
$api->setLogger(new CurlLogger());

$fields = array(
);
$params = array(
);
echo json_encode((new Lead($id))->getSelf(
  $fields,
  $params
)->getResponse()->getContent(), JSON_PRETTY_PRINT);

it outputs: Call to undefined method FacebookAds\Object\Lead::getContent()

emul8sw commented 5 years ago

Don't know if you ever came right, but I found a solution for anybody also struggling with this.

$ThisLead = (new Lead($id))->getSelf($fields,$params)->exportAllData();

I am currently struggling to encode the above to proper json, but the following might also help someone:

$LeadCreatedTime = $ThisLead['created_time'];
$LeadID = $ThisLead['id'];
foreach ($ThisLead['field_data'] as $key => $value) {
 print_r($value['name']);
 print_r(': ');

 print_r($value['values'][0]);
 print_r('<br>');
}

Just do a var_dump on the $ThisLead to see all info. Cannot confirm if this is the correct way of using it, but there is no other solution I could find.

emul8sw commented 5 years ago

Just an update on the above, I realized that piece of code was quite intensive in terms of API calls and rate limiting etc. Below is my working example to get lead data for anyone who is struggling

  //$datefrom used to filter leads where date is greater than X
  if(isset($_GET['datefrom']) && !empty($_GET['datefrom'])){
      $datefrom = $_GET['datefrom'];
  } else {
      $datefrom = "";
  } 

  $ad = new LeadgenForm('1234567890123456');
  if (!empty($datefrom))
  {
    $leads = $ad->getLeads(array(), array(
      AdReportRunFields::FILTERING => array(
        array(
          'field' => 'time_created',
          'operator' => 'GREATER_THAN',
          'value' => $datefrom,
        ),
      ),
    ));            
  }
  else
  {
    $leads = $ad->getLeads();            
  }
  foreach ($leads as $these => $those) 
  {
    //See php-business-sdk/src/FacebookAds/Object/Fields/LeadFields.php for available fields
    $LeadFieldsID = $those->{LeadFields::ID};
    $LeadCreatedTime = $those->{LeadFields::CREATED_TIME};
    print_r('_______________________________________');
    print_r('<br>');
    print_r("Lead ID: ");
    print_r($LeadFieldsID);
    print_r('<br>');

    print_r("Date Created: ");
    print_r($LeadCreatedTime);
    print_r('<br>');

    $LeadFieldsData = $those->{LeadFields::FIELD_DATA};

    foreach ($LeadFieldsData as $key => $value) 
    {
      print_r('<br>');
      print_r($value['name']);
      print_r(': ');

      print_r($value['values'][0]);
      print_r('<br>');
    }
  }  
jingping2015 commented 5 years ago

This will be fix in next release