Skip to content

Instantly share code, notes, and snippets.

@LucasGorgal
Last active August 13, 2021 18:44
Show Gist options
  • Save LucasGorgal/ae65f3694973e8d0ddf2e2d3a32c3f71 to your computer and use it in GitHub Desktop.
Save LucasGorgal/ae65f3694973e8d0ddf2e2d3a32c3f71 to your computer and use it in GitHub Desktop.
conversion api
<?php
function lead_event($pixelid, $access_token, $email) {
$email_sha = hash("sha256", mb_strtolower($email));
$time = time();
$fbc = $_COOKIE['_fbc'];
$fbp = $_COOKIE['_fbp'];
$user_ip = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$event_source_url = 'https://' .$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://graph.facebook.com/v11.0/'.$pixelid.'/events',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('access_token' => $access_token, 'data' => '[
{
"event_name": "'.$event_name.'",
"event_time":'.$time.',
"event_id": "'.$facebook_pixel_unique_id.'",
"user_data": {
"em": [
"'.$email_sha.'"
],
"client_ip_address": "'.$ip.'",
"client_user_agent": "'.$user_agent.'",
"fbc": "'.$fbc.'",
"fbp": "'.$fbp.'"
},
"event_source_url": "'.$event_source_url.'",
"action_source": "website"
},
]', "test_event_code" => "TEST82658")
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error cURL';
}
curl_close($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment