danog / MadelineProto

Async PHP client API for the telegram MTProto protocol
https://docs.madelineproto.xyz
GNU Affero General Public License v3.0
2.8k stars 636 forks source link

getDownloadLink() error message: The given data cannot be sent because it is not serializable #1533

Closed QomariSanjaya closed 1 month ago

QomariSanjaya commented 1 month ago

This is my script downloadLink.php

<?php

if (!file_exists('madeline.php')) {
    copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
include 'madeline.php';

$MadelineProto = new \danog\MadelineProto\API('session.madeline');
$MadelineProto->start();

// Peer to fetch messages from
$peer = '@username';
$limit = 1; // Fetch only the most recent message

// Fetch the most recent message
$array = $MadelineProto->messages->getHistory([
    'peer' => $peer,
    'offset_id' => 0,
    'offset_date' => 0,
    'add_offset' => 0,
    'limit' => $limit,
    'max_id' => 0,
    'min_id' => 0,
    'hash' => 0
]);

// Check if there is a message with media
if (isset($array['messages'][0]['media']['document'])) {
    $message = $array['messages'][0];
    $document = $message['media']['document'];
    $file_id = $document['id'];
    $file_size = $document['size'];
    $file_name = 'file_' . $message['id'];
    $mime_type = $document['mime_type'] ?? 'application/octet-stream';

    try {
        // Generate download link
        $download_link = $MadelineProto->getDownloadLink(
            $file_id,
            'dl.php', // Replace with your server's URL
            size: $file_size,
            name: $file_name,
            mime: $mime_type
        );

        // Output the download link
        echo "Download Link: <a href=\"$download_link\">Download</a>";
    } catch (Exception $e) {
        echo 'Error generating download link: ', $e->getMessage();
    }
} else {
    echo "No media found in the most recent message.";
}

And this is my script dl.php

<?php

if (!file_exists('madeline.php')) {
    copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
include 'madeline.php';

\danog\MadelineProto\API::downloadServer('session.madeline');

Its generate this error message: The given data cannot be sent because it is not serializable.

danog commented 1 month ago

'dl.php' must contain the exact public URL of the download script.