Facebook cURL 用于自定义转化跟踪 API 错误 100

问题描述 投票:0回答:1

我从 Facebook API 收到错误。

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

这是他们的文档:https://developers.facebook.com/docs/marketing-api/conversions-api/using-the-api

我使用 PayLoad Builder 创建我的 PayLoad 并使用 cURL 发布它。

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);`

任何人都可以帮助我理解这个问题吗?据我所知,我正在发送“数据:”

为了完整起见,这里是如果我打印的话会得到什么

$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"}}"

        
curl facebook-conversions-api conversions-api
1个回答
0
投票

我的最终代码是这样的。我希望它对将来的人有所帮助。

$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);

我认为我们只需将内容作为密钥/对发送,并确保它是 JSON 编码的。

© www.soinside.com 2019 - 2024. All rights reserved.