我正在尝试使用Facebook转换API与curl在woocommerce,我的代码如下(访问令牌和像素是隐藏的):
function callAPIFB($method, $url, $data){
$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
add_action( 'woocommerce_add_to_cart', 'facebook_API_addcart');
function facebook_API_addcart () {
$ip_add = WC_Geolocation::get_ip_address();
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$addcart_time = current_time( 'timestamp' );
$total_cart = WC()->cart->cart_contents_total;
$currency = get_woocommerce_currency();
$cart_data_array = array(
"event_name" => "AddToCart",
"event_time" => $addcart_time,
"user_data" => array(
"client_ip_address" => $ip_add,
"client_user_agent" => $user_agent
),
"custom_data" => array(
"currency" => $currency,
"value" => $total_cart
),
"test_event_code" => "TEST72603",
);
$call_fb_cart = callAPIFB('POST', 'https://graph.facebook.com/v9.0/PIXEL/events?access_token=ACCEsS-TOKEN', json_encode($cart_data_array));
?>
<script>
console.log(<?php echo $call_fb_cart;?>);</script>
<?php
}
但是,我在控制台中从FB获得以下错误代码:错误:代码:100条消息:“(#100)参数数据是必需的”类型:“OAuthException”
这是什么原因?
1条答案
按热度按时间wa7juj8i1#
问题是facebook转换api查找您发送的json的“data”参数,但找不到它,而且“data”必须是对象数组,因此代码必须具有以下结构,json_encode才能为其给予该格式