Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to CURL a Facebook ad report

I want to download a Facebook ad report onto my server using CURL because sadly I dont have access to their Ad API and probably never will do.

I successfully logged in to Facebook using CURL (I got an email from Facebook saying my account had been logged in from somewhere else so bingo)

Code Im using for Facebook log in: http://www.daniweb.com/web-development/php/code/290893/facebook-login-with-curl

But the code that downloads the report isn't working. The CSV just shows blank (As it is default on the server)

Any ideas? You can see the URL to the Facebook CSV file isn't direct so this may be part of the problem.... or maybe something with $cookie in the Facebook log in code?

$local_file = "letsbonus-ticket.csv";//This is the file where we save the information
$remote_file = "http://www.facebook.com/ads/manage/download_report.php?act=44309118&report_run_id=6016464099986&format=csv&source=email"; //Here is the file we are downloading


$ch = curl_init();
$fp = fopen ($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);
like image 534
Jake Avatar asked Apr 12 '14 03:04

Jake


1 Answers

Just want to state this is equivalent to scraping in terms of Facebook TOS.

Luckily you don't need to fight the system and play around with cookies.

Facebook Ads API access has been granted to all developers on a tier level.

See https://developers.facebook.com/docs/reference/ads-api/access

You should currently have access to development level

The development access level is designed for development purposes and is ideal for people who are just starting to build out their tool. In this level, you would not yet have customers using your tool. This level is open to all developers, and is intended to build out end-to-end workflows on the API before you get full permissions.

You will be able to use the API on behalf of developers or admins of your app, and you can access up to 5 ad accounts for which those users are admins. To set up the ad account list, refer to the instructions below. Please note that some API calls won't be possible with Development or Basic access because they're potentially associated with multiple accounts, or the affected account can't be identified programmatically.

So using any app you control along with any ad account you own while granting yourself ads_read and/or ads_management permission (depending on what you need) make an API call to

/me/adaccounts and from there you should be able to get ad account info, in your case you can call /act_44309118 or e.g. /act_44309118/stats

See https://developers.facebook.com/docs/reference/ads-api/overview for a full listing of all that is available to you.

like image 144
phwd Avatar answered Nov 08 '22 14:11

phwd