I have generated the statistics of my Facebook campaigns with the API Marketing and php ads sdk , I get access to my report and from the browser what I can download by entering an url like this:
www.facebook.com/ads/ads_insights/export_report?report_run_id=REPORT_ID&format=csv&access_token=TOKEN
$fileName = "campaign_insights.csv";
$graph_url = 'https://www.facebook.com/ads/ads_insights/export_report?report_run_id='.$report_run.'&format=csv&access_token='.$access_token;
$path = "campaign_insights.csv";
set_time_limit(0);
$fp = fopen ($path, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
$result = curl_exec( $ch );
var_dump($result); //siempre responde false
curl_close($ch);
fclose($fp);
My problem is when I try to save that generated file (csv) directly on my server, I tried to do it with cURL
, file_get_contents
but I have not succeeded, the cURL
answers me empty and file_get_contents
Facebook does not allow it, any idea of how I could do to save this file on my server?
Thanks.
UPDATE:
The solution is below.