【发布时间】:2021-05-21 15:14:03
【问题描述】:
我只是想对 Facebook Graph API 进行最基本的测试。我正在使用 PHP 和 cURL。我直接从 Facebook Payload Helper 复制了数据字符串。一切似乎都正确,但我收到了消息:
string(135) "{"error":{"message":"(#100) 参数数据为必填项","type":"OAuthException","code":100,"fbtrace_id":"Age1fgb47kngho3guOkcAcj “}}”
消息告诉我“数据”参数没有设置,但是你可以看到它是在数据字符串中设置的。请参阅下面的代码。任何调试此问题的帮助将不胜感激。
<?php
$url = "https://graph.facebook.com/v10.0/<PIXEL ID>/events?access_token=<TOKEN>";
$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: multipart/form-data",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{
"data": [
{
"event_name": "Purchase",
"event_time": '.time().',
"action_source": "website",
"event_id": '.rand(100000, 500000).',
"user_data": {
"em": "6b583232e99af45c3b436798f32800a4dd6f3524624ba6507ed8269b53ce82a3",
"ph": null
},
"custom_data": {
"currency": "USD",
"value": "37"
}
}
],
"test_event_code":"TEST9031"
}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
【问题讨论】:
-
数据似乎是 JSON,但您发送的
Content-Type标头不匹配。 -
感谢您的回复。我把它改成了“content-type:application/json”,没有任何区别。
-
“我直接从 Facebook Payload Helper 复制了数据字符串。 ” - 我在那里得到类似
curl -X POST --form-string 'data=[{"action_source": …的东西,而你的版本似乎有一组额外的(不是双关语)花括号?尝试不使用这些。
标签: php api facebook curl graph