CURL PHP Example for Facebook Conversion API

Send Your Facebook Conversion with a Simple PHP CURL Snippet. Avoid using Facebook SDK and get started with a lean solution.

This is a small snippet to get started with CURL in PHP without installing any PHP SDK. A simple CURL command for everyday event tracking.

$data = array( // main object
    
    
    "data" => array( // data array
        
        array(
            "event_name" => "Purchase",
            "event_time" => time(),
               "user_data" => array(
                "client_ip_address" => $ip,
                "client_user_agent" => $browser,
                "em" => $email,
                "ph" => $phone,
                "fbc" =>'',
                "fbp" =>''
                ),
                "contents" => array(
                "id" => $item_ids,
                "quantity" => count($item_qty),
                "delivery_category"=> "home_delivery"
                ),
                "custom_data" => array(
                "currency" => "GBP",
                "value"    => $order_total,
                ),
                "action_source" => "website",
                "event_source_url"  => $fullURL,
       ),
    ),
       "access_token" => "{Access Token}"

    );  
    
    
    $dataString = json_encode($data);                                                                                                              
    $ch = curl_init('https://graph.facebook.com/v11.0/{PIxel ID}/events');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($dataString))                                                                       
    );                                                                                                                                                                       
    $response = curl_exec($ch);

Enjoy!