stackuser
stackuser

Reputation: 198

Get all lead ads form facebook API

I am trying to get all lead generation forms in my PHP script using Facebook API version 9.0

I am making the request like this:

https://graph.facebook.com/v9.0/<PAGE_ID>/leadgen_forms?<access_token>

But its returning error, saying

{
   "error": {
      "message": "(#12) leadgen_forms field is deprecated for versions v5.0 and higher",
      "type": "OAuthException",
      "code": 12,
      "fbtrace_id": "ARMp3WCko2raN5521458h"
   }
}

If I change the version in the above request, it says:

You are calling a deprecated version of the Ads API. Please update to the latest version: v9.0.

UPDATE:

Here's the SDK function which i am using to get Lead Generation Forms:

public function getLeadGenForms(array $fields = array('first_name', 'last_name'), array $params = array(), $pending = false) {
    $this->assureId();

    $param_types = array(
      'query' => 'string',
    );
    $enums = array(
    );

    $request = new ApiRequest(
      $this->api,
      $this->data['id'],
      RequestInterface::METHOD_GET,
      '/leadgen_forms',
      new LeadgenForm(),
      'EDGE',
      LeadgenForm::getFieldsEnum()->getValues(),
      new TypeChecker($param_types, $enums)
    );
    $request->addParams($params);
    $request->addFields($fields);
    return $pending ? $request : $request->execute();
  }

Upvotes: 4

Views: 4180

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43491

This is the correct GET request URL:

https://graph.facebook.com/v9.0/<PAGE_ID>/leadgen_forms?access_token=<page_access_token>

And example use of the FB PHP SDK:

try {
  // Returns a `FacebookFacebookResponse` object
  $response = $fb->get(
    '/<PAGE_ID>/leadgen_forms',
    '{access-token}'
  );
} catch(FacebookExceptionsFacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
$graphNode = $response->getGraphNode();

Source: Generated from the Graph API Explorer.

Screenshot of a working error-free response: enter image description here

Requirements:

  • Use the page access token of the page <PAGE_ID> (not your own user token or any other page).
  • Grant the pages_show_list, pages_read_engagement and pages_manage_ads permissions to the FB app that you connect to (to get the page access token).

Upvotes: 2

Related Questions