Issue with Uploading Cover Photo for Lead Generation Form using PHP SDK

47 Views Asked by At

I'm experiencing an issue with the Facebook PHP SDK when trying to upload a cover photo for a lead generation form. Despite following the SDK documentation and examples, I keep encountering this error

(#100) Param cover_photo must be a file with one of the following types: image/jpeg, image/gif, image/png. Received file of type 'application/octet-stream'.

Here's a snippet of my code:

$data = array_filter(
     array(
         LeadgenFormFields::NAME => $task['name'],
         LeadgenFormFields::QUESTIONS => $task['questions'],
         LeadgenFormFields::FOLLOW_UP_ACTION_URL => $task['followUpActionUrl']
         )
);

if (isset($task['coverPhotoUrl'])) {
    // Code to download and save the image to a temporary file
    $tmpFile = FacebookHelper::getTemporaryFileWithName($contentUrl, $name);
    $fileContent = file_get_contents($contentUrl);
    
    if (file_put_contents($tmpFile["filename"], $fileContent) === false) {
        throw new Exception("Failed to write the cover photo to temporary file: " . $tmpFile["filename"]);
    }

    // Passing the file path directly
    $data["cover_photo"] = $tmpFile["filename"];
}

$page = new Page($task["facebookPageId"]);
$leadForm = $page->createLeadGenForm([], $data);

The issue persists even after ensuring the file is correctly downloaded and exists. I've tried using CurlFile and directly passing the file path, but neither approach works. CurlFile approach was throwing "operation aborted by callback" error.

The SDK's method createLeadGenForm is used for this operation.

Could you please provide guidance on the correct approach to upload a cover photo using the PHP SDK? Any insights or examples would be greatly appreciated.

Thank you.

0

There are 0 best solutions below