curlfacebook-conversions-apiconversions-api

Facebook cURL for custom conversion tracking API error 100


I'm getting an error from Facebook API.

string(135) "{"error":{"message":"(#100) The parameter data is required","type":"OAuthException","code":100,"fbtrace_id":"AGpuvWfJHaG5Dyx4ffRZ_e-"}}"

Here is their documentation: https://developers.facebook.com/docs/marketing-api/conversions-api/using-the-api

I have used the PayLoad Builder to create my PayLoad and used cURL to post it.

https://developers.facebook.com/docs/marketing-api/conversions-api/payload-helper/

`    $pixelID = "MY PIXEL ID";
    $token = "MY TOKEN";
    $url = "https://graph.facebook.com/v10.0/$pixelID/events?access_token=$token";

$testEvent = "MY TEST ID";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
    // "content-type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = '{
            "data": [
                {
                    "event_name": "Purchase",
                    "event_time": ' . $timestamp . ',
                    "action_source": "website",
                    "user_data": {
                        "em": [
                            "' . $email . '"
                        ],
                        "ph": [
                            "' . $phonenumber . '"
                        ],
                        "fn": [
                            "' . $firstName . '"
                        ],
                        "ln": [
                            "' . $lastName . '"
                        ],
                        "ct": [
                            "' . $city . '"
                        ],
                        "st": [
                            "' . $county . '"
                        ],
                        "zp": [
                            "' . $postCode . '"
                        ],
                        "country": [
                            "0b407281768f0e833afef47ed464b6571d01ca4d53c12ce5c51d1462f4ad6677"
                        ]
                    },
                    "custom_data": {
                        "currency": "GBP",
                        "value": "' . (float)$paytotal . '"
                    }
                }
            ]
        },
    "test_event_code":"' . $testEvent . '"
';

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);`

Can anyone help me to understand the issue? So far as I can tell I am sending "data:"

For completeness here is what arrives if I print $data

{
                "data": [
                    {
                        "event_name": "Purchase",
                        "event_time": 1712266200,
                        "action_source": "website",
                        "user_data": {
                            "em": [
                                "74fede2a4644b6d958b33e48d085c6dcf1434cf74ac29486c824c64c62f4341b"
                            ],
                            "ph": [
                                ""
                            ],
                            "fn": [
                                "068b525cc8cebb28ec7f75c1814c9f9725ccb7a76e6f149049603f504ca5b668"
                            ],
                            "ln": [
                                "7efa869d0364eea0cd0106a2ef4d1ae9eaec58fe62928c3f1af8fa8da9204ea0"
                            ],
                            "ct": [
                                "0906246e56d65f9c0cc0929544494b52598175ebb86932e28727f1ebef16137e"
                            ],
                            "st": [
                                "c153d60ae29ef1a1577a09285cece714e80f02904eb9a244e92d60d199901b9c"
                            ],
                            "zp": [
                                "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
                            ],
                            "country": [
                                "0b407281768f0e833afef47ed464b6571d01ca4d53c12ce5c51d1462f4ad6677"
                            ]
                        },
                        "custom_data": {
                            "currency": "GBP",
                            "value": "112.5"
                        }
                    }
                ]
            },
        "test_event_code":"TEST7672"
    string(135) "{"error":{"message":"(#100) The parameter data is required","type":"OAuthException","code":100,"fbtrace_id":"AhL2jsc1CX21JzfetlQMjwZ"}}"

        

Solution

  • My final code was this. I hope it helps someone in the future.

    $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            $headers = array(
                "content-type: application/json",
            );
                
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
                
            $data = [
                'data' => [
                    [
                        'event_name' => 'Purchase',
                        'event_time' =>  time(),
                        'action_source' => "website",
                        'user_data' => [
                            'em' => [$email],
                            'ph' => [$phonenumber],
                            'fn' => [$firstName],
                            'ln' => [$lastName],
                            'ct' => [$city],
                            'st' => [$county],
                            'zp' => [$postCode],
                            'country' => [
                                "0b407281768f0e833afef47ed464b6571d01ca4d53c12ce5c51d1462f4ad6677"
                            ]
                        ],
                        'custom_data' => [
                            'currency' => 'GBP',
                            'value' => (float)$totalPaid
                        ]
                    ]
                ]//,
                // 'test_event_code' => $testEvent
            ];
            
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
            // print_r($data);
            $resp = curl_exec($curl);
            curl_close($curl);
            // var_dump($resp);
    

    I think we just had to send things as key/pair and make sure it was JSON encoded.