PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 18, 2022

[FIXED] How to download media in Twilio Whatsapp using PHP?

 November 18, 2022     php, twilio, twilio-api, twilio-php     No comments   

Issue

So I am using Twilio Whatsapp API and need to down load media using PHP. I can pickup the https://api.twilio..... address that reroutes to an aws address - therefore downloading using the below code wont work as it tries to download from the api.twilio URL and not the aws address that its routed to:

 $url = $URLFile;
    
    
    // Initialize the cURL session
    $ch = curl_init($url);
    
    // Initialize directory name where
    // file will be save
    // $dir = './';
    $dir = $dirname;
    
    // Use basename() function to return
    // the base name of file
    $file_name = basename($url);
    
    // Save file into file location
    $save_file_loc = $dir . $file_name;
    
    // Open file
    $fp = fopen($save_file_loc, 'wb');
    
    // It set an option for a cURL transfer
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    // Perform a cURL session
    curl_exec($ch);
    
    // Closes a cURL session and frees all resources
    curl_close($ch);
    
    // Close file
    fclose($fp);

Solution

You'll want to use CURLOPT_FOLLOWLOCATION to follow redirects with cURL.

You can do this:

 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

and your code will follow the redirect.



Answered By - William Evans
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing