0

I am trying to send events to the Facebook conversion api. I have it working OK using cURL but would rather use guzzle. However, when i try to do the same with guzzle I get an error in the response.

Can anyone assist with getting this to work with guzzle?

Here is the event data

$data = array(
            "data" => array(
                "test_event_code" => "TEST22801",
                "event_name" => $eventName,
                "event_time" => $this->time,
                "action_source" => $this->actionSource,
                "event_source_url" => $this->eventUrl,
                "event_id" => $eventID,
                "user_data" => array(
                    "em" => $email,
                    "ph" => $phone,
                    "fn" => $firstName,
                    "ln" => $surname,
                    "client_ip_address" => $this->ip,
                    "client_user_agent" => $this->browser,
                    "fbp" => $this->fbp,
                    "fbc" => $this->fbc,
                ),
            ),
            "access_token" => {accessToken},
        );

        $body = json_encode($data);

This cURL request works:

        $ch = curl_init('https://graph.facebook.com/v15.0/{Pixel}/events');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($body))
        );
        $response = curl_exec($ch);

But this guzzle request doesn't:

$client = new GuzzleHttp\Client([
            'timeout' => 1.5,
        ]);

        // this doesn't work with guzzle
        $client->request('post', 'https://graph.facebook.com/v15.0/{Pixel}/events', [
            'body' => $body,
            'headers' => [
                'Content-Type' => 'application/json',
                'Content-Length' => strlen($body)
            ]

        ]);

and returns this error in the response:

{"error":{"message":"(#100) param data must be an array.","type":"OAuthException","code":100,"fbtrace_id":"A79KkZQwrNIUK}}

0 Answers0