0
votes

I am trying to export the insights csv report using Facebook Marketing API

https://developers.facebook.com/docs/marketing-api/insights/async/v2.8

By sending POST request to /insights endpoint, I am able to successfully get the AD_REPORT_RUN_ID and manually downloading the report from browser works fine but when trying to download the report via php curl I am getting empty response.

Here is my curl code:-

        $fileName = "campaign_insights.csv";
        $reportDownloadUrl = "https://www.facebook.com/ads/ads_insights/export_report/?report_run_id=<AD_REPORT_RUN_ID>&access_token=<ACCESS_TOKEN>&name=campaigns_insights&format=csv";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $reportDownloadUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSLVERSION,6);
        $result = curl_exec($ch);
        if(curl_error($ch))
        {
            echo 'Error Occured :' . curl_error($ch);
        }
        curl_close($ch);
        $fp = fopen($fileName, 'w');
        fwrite($fp, $result);
        fclose($fp);

Any idea how to get this worked?

1
if you print_r(json_decode($result)) is that empty? also do they recommend the 2 SSL options you can used? - noelnoegdip

1 Answers

0
votes

You can get the async insights results by calling the endpoint https://graph.facebook.com/v2.8/AD_REPORT_RUN_ID/insights

The reason why you got empty response is that https://www.facebook.com/ads/ads_insights/export_report is not part of the graph API and it uses a different authentication method. It doesn't read the access_token in params.