我试图根据这里的说明编写PHP脚本:
https://developers.facebook.com/ads/blog/post/2016/12/18/lead-ads-offline/
但是我遇到了一个json字符串问题,它在PHP中作为参数传递给curl。看起来它正在添加反斜杠(&#34; match_keys&#34;:&#34;无效密钥\&#34; email \&#34;等等)导致API调用失败。< / p>
我试过玩:
json_encode($array,JSON_UNESCAPED_SLASHES);
我已经尝试了一堆SO答案,就像Curl add backslashes但没有运气。
<?php
$email = hash('sha256', 'test@gmail.com');
$data = array("match_keys" => '{"email": $email}',
"event_time" =>1477632399,
"event_name"=> "Purchase",
"currency"=> "USD",
"value"=> 2.00);
$fields = [
// 'upload_tag'=>'2016-10-28-conversions',
'access_token'=>'#######',
'data'=> $data
];
$url = 'https://graph.facebook.com/v2.8/#######/events';
echo httpPost($url, $fields);
function httpPost($url, $fields)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
?>
这是回复:
Array{"error":{"message":"(#100) Invalid parameter","type":"OAuthException","code":100,"error_data":{"match_keys":"Invalid keys \"email\" were found in param \"data[match_keys]\".","event_time":"Out of bounds array access: invalid index match_keys","event_name":"Out of bounds array access: invalid index match_keys","currency":"Out of bounds array access: invalid index match_keys","value":"Out of bounds array access: invalid index match_keys"},"fbtrace_id":"BrVDnZPR99A"}}%
答案 0 :(得分:0)
您正在将JSON与PHP数组混合使用。我怀疑它实际上是您打算用来发送数据的JSON,因为您正在使用http_build_query
。试试这个:
$data = array("match_keys" => ["email" => $email],
"event_time" =>1477632399,
"event_name"=> "Purchase",
"currency"=> "USD",
"value"=> 2.00
);
这样,您可以将数据定义为数组,并让http_build_query
为您进行编码。