简体   繁体   中英

Facebook Graph API PHP and cURL

I am just trying to get the most basic test of the Facebook Graph API working. I am using PHP and cURL. I copied the data string directly from the Facebook Payload Helper. Everything appears correct, but I am receiving the message:

string(135) "{"error":{"message":"(#100) The parameter data is required","type":"OAuthException","code":100,"fbtrace_id":"Age1fgb47kngho3guOkcAcj"}}"

The message is telling me that the "data" parameter is not set, but you can see that it is set in the data string. See my code below. Any help in debugging this would be greatly appreciated.

<?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);
?>

If it can help, I did a class which contains this method to send data to Facebook's API:

/** @var string url to accede to Facebook API */
private $apiUrl = "https://graph.facebook.com/v12.0";

/** @var int id of pixel used with Facebook API */
private $pixelId = 0000000000000;

/** @var string token to accede to Facebook API */
private $token = "QWERTY...";

/** @var string|null code to specify test to Facebook API */
private $testEventCode = "TEST00000";

/** Send datas to Facebook's API
 *
 * @param array datas to send like ['event_name' => 'ViewContent', ... ]
 *
 * @return string|bool
 */
public function sendData(array $data)
{
    $fields = [
        'access_token' => $this->token,
        'test_event_code' => $this->testEventCode,
        'data' => [$data],
    ];

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => "{$this->apiUrl}/{$this->pixelId}/events",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($fields),
        CURLOPT_HTTPHEADER => [
            "cache-control: no-cache",
            "accept: application/json",
            "content-type: application/json",
        ],
    ]);

    return curl_exec($ch);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM