How to get campaign name from campaign id using facebook sdk?

Member

by zion , in category: Social Media SEO , a year ago

How to get campaign name from campaign id using facebook sdk?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by jamison , a year ago

@zion 

To get the campaign name from a campaign ID using the Facebook SDK, you can use the Marketing API. Here's an example of how you can do it in PHP using the Facebook PHP SDK:

  1. First, make sure you have installed the Facebook PHP SDK using Composer or by manually including the SDK files.
  2. Include the necessary classes and initialize the Facebook SDK:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
require_once __DIR__ . '/vendor/autoload.php';

use FacebookFacebook;

// Initialize the Facebook SDK
$fb = new Facebook([
    'app_id' => 'YOUR_APP_ID',
    'app_secret' => 'YOUR_APP_SECRET',
    'default_graph_version' => 'v11.0',
]);


Make sure to replace 'YOUR_APP_ID' and 'YOUR_APP_SECRET' with your actual Facebook app credentials.

  1. Use the Marketing API to retrieve the campaign name:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$campaignId = 'CAMPAIGN_ID';

try {
    $response = $fb->get('/' . $campaignId . '?fields=name', '{access-token}');
    $campaign = $response->getGraphObject()->asArray();

    $campaignName = $campaign['name'];
    echo 'Campaign Name: ' . $campaignName;
} catch (FacebookExceptionsFacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
} catch (FacebookExceptionsFacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
}


Make sure to replace 'CAMPAIGN_ID' with the actual campaign ID you want to retrieve the name for and 'access-token' with a valid access token.


The above code sends a GET request to the Marketing API with the specific campaign ID and requests only the name field. The campaign name is then extracted from the response and stored in the $campaignName variable.


Remember to have the appropriate permissions to access the campaign details based on the access token used.