Back

Step by Step Guide to Building a LinkedIn Ads API Integration in PHP

Aug 1, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of LinkedIn Ads API? You're in for a treat. We'll be using the samoritano/linkedin-api-php-client-v2 package to make our lives easier. This nifty tool will help us navigate the LinkedIn Ads API like pros. Let's get started!

Prerequisites

Before we jump in, make sure you've got these basics covered:

  • A PHP environment that's up and running
  • Composer installed (trust me, it's a lifesaver)
  • A LinkedIn Developer account with API credentials (if you don't have this, hop over to LinkedIn's developer portal and set it up)

Got all that? Great! Let's move on.

Installation

First things first, let's get our package installed. Open up your terminal and run:

composer require samoritano/linkedin-api-php-client-v2

Easy peasy, right? Now we're cooking with gas!

Authentication

Alright, time to get authenticated. We'll be using OAuth 2.0 for this dance:

use LinkedIn\Client; $client = new Client('your_client_id', 'your_client_secret'); $callback = 'http://your-callback-url.com'; $scopes = ['r_liteprofile', 'r_ads', 'w_ads']; $url = $client->getAuthorizationUrl($callback, $scopes); // Redirect the user to $url... // After the user authorizes, you'll get a code. Use it to get an access token: $accessToken = $client->getAccessToken($code);

Boom! You're in. Keep that access token safe; you'll need it.

Basic API Usage

Now that we're authenticated, let's make some API calls:

$client->setAccessToken($accessToken); // Get account info $account = $client->get('adAccountsV2');

See how easy that was? You're a natural!

Working with Ads

Let's create an ad campaign:

$campaign = [ 'account' => 'urn:li:sponsoredAccount:123456789', 'name' => 'My Awesome Campaign', 'objective' => 'WEBSITE_VISITS', // ... other campaign details ]; $response = $client->post('adCampaignsV2', $campaign);

Managing creatives and setting up targeting options follows a similar pattern. Just check the API docs for the specific endpoints and parameters.

Reporting and Analytics

Want to see how your ads are performing? Let's fetch some data:

$metrics = ['impressions', 'clicks', 'costInUsd']; $timeRange = ['start.day' => '2023-01-01', 'end.day' => '2023-12-31']; $report = $client->get('adAnalyticsV2', [ 'campaigns' => ['urn:li:sponsoredCampaign:12345'], 'dateRange' => $timeRange, 'fields' => $metrics ]);

Error Handling and Best Practices

Remember to handle those pesky rate limits:

try { $response = $client->get('someEndpoint'); } catch (\LinkedIn\Exception\LinkedInException $e) { if ($e->getCode() == 429) { // Rate limit hit. Wait and retry. sleep(60); $response = $client->get('someEndpoint'); } }

And always check for error responses. The API will usually give you helpful error messages if something goes wrong.

Advanced Topics

Want to level up? Look into webhook integrations for real-time updates, or explore bulk operations for managing large-scale campaigns. The world's your oyster!

Conclusion

And there you have it! You're now equipped to build a killer LinkedIn Ads API integration. Remember, practice makes perfect, so don't be afraid to experiment and push the boundaries.

For more in-depth info, check out the LinkedIn Marketing API documentation and the samoritano/linkedin-api-php-client-v2 GitHub repo.

Now go forth and conquer the world of LinkedIn Ads! You've got this. 💪