facebookarchive / php-graph-sdk

The Facebook SDK for PHP provides a native interface to the Graph API and Facebook Login. https://developers.facebook.com/docs/php
Other
3.17k stars 1.95k forks source link

Attacthing file to a message #1175

Closed yemkay closed 4 years ago

yemkay commented 4 years ago

I'd like to attach a file from local disk while sending message.

API documentation has this curl example. Is this possible with PHP wrapper?

curl \ -F 'recipient={"id":"<PSID>"}' \ -F 'message={"attachment":{"type":"<ASSET_TYPE>", "payload":{"is_reusable"=true}}}' \ -F 'filedata=@/tmp/shirt.png;type=image/png' \ "https://graph.facebook.com/v5.0/me/messages?access_token=<PAGE_ACCESS_TOKEN>"

https://developers.facebook.com/docs/messenger-platform/send-messages#file

yemkay commented 4 years ago

It seems to be supported. Sample code,

          $params['message']['attachment'] = array(
                'type' => 'image',
                'payload' => array(
                    'is_reusable' => true
                )
            );
            $params['source'] = $fb->fileToUpload($imagePath); //path to an image file
            $fb->post('/me/messages', $params, $accessToken);
kenny-lee-1992 commented 4 years ago

Same problem,

@yemkay , can you show me your solution or just use CURL for this case ?

Thanks,

kenny-lee-1992 commented 4 years ago
$endpoint = "https://graph.facebook.com/v7.0/me/message_attachments?access_token=yourAT";
$cfile = new \CURLFile($_FILES['fileTest']['tmp_name'],$_FILES['fileTest']['type'],'test_name');
$post     = [
    "message" => json_encode([
        "attachment" => [
            "type"    => 'image',
            "payload" => [
                "is_reusable" => true,
            ],
        ],
    ]),
    'filedata' => $cfile
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$result = curl_exec($ch);
curl_close($ch);
yemkay commented 4 years ago

This is what worked for me:

public function uploadPhoto($pageId, $imagePath, $doPublish=false) {
        return $this->_post($pageId.'/photos', array(
            'source' => $this->fb->fileToUpload($imagePath),
            'published' => $doPublish? 'true':'false'
        ));
    }

    protected function _execute($method, $endpoint, $params=array()) {
        try {
            log_message('info', 'FB API request: ' . $endpoint .', Params: '.print_r($params, true));
            $response = $this->fb->$method($endpoint, $params, $this->accessToken);
            log_message('info', 'FB API response: ' . print_r($response, true));
        } catch (\Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            log_message('error', 'Error FacebookResponseException: ' . $e->getMessage());
            log_message('error', 'Exception: '.print_r($e, true));
            throw $e;
        } catch (\Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            log_message('error', 'Error FacebookSDKException: ' . $e->getMessage());
            log_message('error', 'Exception: '.print_r($e, true));
            throw $e;
        }
        return $response;
    }

    protected function _post($endpoint, $params) {
        return $this->_execute('post', $endpoint, $params);
    }
kenny-lee-1992 commented 4 years ago

Thank so much