How to download Media from the Meta API (Whatsapp Media)

166 views Asked by At

I have followed the documentation on the official link and retrieved the Media URL https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media/#download-media

Now once I have the URL, I am using the below PHP code to download the file that is on saved on FB Servers which needs an authorisation token to be sent so I use CURL in PHP.

However I am getting empty files saved.

$ch             = curl_init($url);
$file_name      = basename($url);
$file_loc       = "./download/" . $MediaID . ".jpg" ;
$fp             = fopen($file_loc, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $MetaToken) );
$result = curl_exec($ch); curl_close($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
fwrite($fp, $body);
fclose($fp);

I have also tried it like this:

$file_loc   = "./download/" . $MediaID . ".jpg" ;
$fp         = fopen($file_loc, 'wb');
$curl       = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => array('Authorization: Bearer ' . $waToken),
    ));
    $response = curl_exec($curl); curl_close($curl);
    fwrite($fp, $response); fclose($fp);
1

There are 1 answers

0
izzymo On

Phew! It finally worked by changing the user-agent

            $curl       = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_USERAGENT => "YourAppName/2.0",
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'GET',
                CURLOPT_HEADER => true,
                CURLOPT_HTTPHEADER => array('Authorization: Bearer ' . $waToken),
            ));
            $result = curl_exec($curl); curl_close($curl);
            list($headers, $content) = explode("\r\n\r\n", $result, 2);
            foreach (explode("\r\n", $headers) as $hdr) {
                if ( strpos($hdr, "Content-Disposition") !== false ) { $ext = substr(strrchr($hdr, '.'), 1); }
            }
            $file_loc   = "./download/" . $MediaID . "." . $ext ;
            $fp         = fopen($file_loc, 'wb');
            fwrite($fp, $content); fclose($fp);