Skip to content

Instantly share code, notes, and snippets.

@jice-lavocat
Last active August 10, 2017 10:02
Show Gist options
  • Save jice-lavocat/163d5b8f269a08b65ac6a66f38905d71 to your computer and use it in GitHub Desktop.
Save jice-lavocat/163d5b8f269a08b65ac6a66f38905d71 to your computer and use it in GitHub Desktop.
Send offline events to Facebook API by using PHP curl
<?php
// Replace with your access token
$access_token = 'your_access_token';
// PURCHASE DATA
// Replace with your own customer/purchase data
$data = array();
$data["match_keys"]["lead_id"] = "1234";
$data["event_time"] = 1477632399;
$data["event_name"] = "Purchase";
$data["currency"] = "USD";
$data["value"] = 2.00;
// Turn Data to JSON
$data_json = json_encode(array($data));
// Fill available fields
$fields = array();
$fields['access_token'] = $access_token;
$fields['upload_tag'] = "orders"; // You should set a tag here (feel free to adjust)
$fields['data'] = $data_json;
$ch = curl_init();
curl_setopt_array($ch, array(
// Replace with your offline_event_set_id
CURLOPT_URL => "https://graph.facebook.com/v2.10/your_offline_event_set_id/events",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query($fields),
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: multipart/form-data",
"Accept: application/json" ),
));
$result = curl_exec($ch);
echo "\nResult encode";
echo ($result);
die();
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment