Home > Blockchain >  Error whatspp cloud api save image cURL PHP
Error whatspp cloud api save image cURL PHP

Time:08-05

I am having a problem downloading images of a message sent by the user in whatsapp cloud api, when performing a test in postman, if I get the image.

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => 
'https://lookaside.fbsbx.com/whatsapp_business/attachments/? 
mid=xxxxxxxxxxxxxxx&ext=xxxxxxxxxxxxxxx&hash=xxxxxxxxxxxxxxx',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

enter image description here

but on my server when executing the cURL code in php it redirects me to.

enter image description here

CodePudding user response:

I also had this problem, then I added the user-agent and I went ahead, however, I still can't download the image getting as a response "something went wrong"

add this line to the curl options:

CURLOPT_USERAGENT=>'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'),

I hope you find it useful and if you can download the image, please help me too...

CodePudding user response:

I have managed to download the image by applying this example of Omar Tamman

Whatsapp Business Cloud API returning empty string when trying to download media

my example that worked:

$ch = curl_init($rptaUrlMedia);

$token = "xxxxxxxxxxxxx";

$output_filename = 'demo.png';
$fp = fopen($output_filename, 'wb');
    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST , "GET");
curl_setopt($ch,CURLOPT_ENCODING , "");
curl_setopt($ch,CURLOPT_FILE , $fp);

$headers = [];
$headers[]  = "Authorization: Bearer " . $token;
$headers[]  = "Accept-Language:en-US,en;q=0.5";
$headers[]  = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$raw = curl_exec($ch);
    
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

demo.png is an image file already created where you save the result

What I am seeing now is to be able to identify what type of file is image, video, audio...

if someone finds how to identify the type of file, please share it

  • Related