I am trying to use the Facebook conversions api with curl in woocommerce, my code is as follows (access token and pixel are hidden):
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
}
However I get the following error code in the console from FB: error: code: 100 message: "(#100) The parameter data is required" type: "OAuthException"
What could this be due to?
the problem is that the facebook conversions api looks for the "data" parameter of the json that you send and cannot find it, also "data" has to be an array of objects, therefore the code must have the following structure for json_encode to give it that format