I'm using the following lines of code (PHP) after successfuly retriving the media URL and then storing it in the $mediaURL variable for the file request, but it's returning an empty string. Already tried with postman and it returns a 500 internal server error...

** Edited **

self::writeLog('Media URL: '.$mediaURL); self::writeLog('Preparing to download media - id: '.$media_id); $curl = curl_init($mediaURL); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = array(     "Authorization: Bearer ".self::$auth_token, ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); if (($resp = curl_exec($curl)) === false) {     self::writeLog('cURL Error: '.curl_error($curl)); } else if ($resp == '') {     self::writeLog('Empty string.');     self::writeLog('URL: '.$mediaURL);     self::writeLog('Headers: '.$headers[0]); } else {     self::writeLog($resp); }              
  • writeLog is just a method that I use to write these messages on a txt file.

Tag:php, api, curl, whatsapp

3 comments.

  1. Omar Tammam

    i faced this issue before and the reason was not passing User-Agent to the API lead to wrong values returns

    I used the following method to download whatsapp-cloud media & it works fine with me

    public function grabImage(string $url,string $saveto) : bool{ $ch = curl_init($url); 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 , ""); $headers = []; $headers[] = "Authorization: Bearer " . self::$adminToken; $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); if((int)$httpcode == 200){ // here save the $row content of file } return false; }
    1. Rafael Bitencourt

      Thank you so much! The user agent alone did the trick :)

    2. Omar Tammam

      most welcome! I have some little experience with WhatsApp-cloud & WhatsApp on-premises integrations, so please feel free to contact me anytime you face any issue with it

Add a new comment.