I am using Twilio's Profgrammable Fax API to get Faxes and want to store the PDF on my local server.
I get to the point where I get the various parameters about my fax, including $_REQUEST['MediaUrl'] which contains a link to the S3 URL where the PDF is stored. However, when I try to download it with CURL (which works for every other URL PDF), I get a 0 byte file (doesn't work).
Here is the PHP code I use. Can anyone help or provide an alternate PHP code to get the FAX media?
Note: $fax_faxid is the ID from Twilio and $fax_media is the $_REQUEST['MediaUrl'] value
$path = "faxes/".$fax_faxid.".pdf";
$ch = curl_init($fax_media);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $fax_media);
$data = curl_exec($ch);
curl_close($ch);
$result = file_put_contents($path, $data);
if(!$result){echo "error";}else{echo "success";}
I also tried to have Twilio send me the media inside $_REQUEST['Media'] and tried to write that to a file via...
$file = fopen("MEDIA.pdf", "w") or die("Unable to open file!");
fwrite($file, $fax_media);
fclose($file);
but it doesn't work.
HERE Is a code that I got from Twilio Support.... Can anyone translate it into PHP from Python?
@app.route('/receiveAction', methods=['POST'])
def receiveAction():
faxMedia = request.form['MediaUrl']
fileReq = requests.get(faxMedia, stream=True)
with open('localFile.pdf', 'wb') as f:
f.write(fileReq.content)
return ('file downloaded locally!', 200)