I am using a HTML5 player to play a video on website, the video is grabbed from CDN. By Using CURL I am getting the file from CDN but when I try to play that in browser I am not able to play it, so I think that headers are not set properly for the file.
Here is my code:
if (!empty($path) && $path != null)
{
$ch = curl_init($path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false)
{
echo 'CURL Failed';
exit;
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches))
{
$contentLength = (int) $matches[1];
}
header('Content-Transfer-Encoding: binary');
header('Content-Type: video/mp4');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . $contentLength);
ob_clean();
flush();
echo $data;
exit;
}
Are the headers set properly to play a MP4 file? Cause when I hit the URL video is not played. The video file is MP4 and I am trying to play this file in chrome(chrome supports mp4 file). The direct URL access to file is http://moviese.s3.amazonaws.com/videos/test7.mp4
Related
I made a script where depending on the request the server will return a specific file for download from an external source, I use an external source because I have unlimited bandwidth on the other server:
$ch = curl_init();
$url="http://www.example.com/downloads/$fileName";
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true); // make it a HEAD request
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$mimeType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mimeType");
header("Content-length: $size");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition:attachment;filename='$fileName'");
readfile($url);
$filename comes from the request on the front end. It is a simple post from a form.
Everything works great, but some users, not all, have reported that they cant open the file because the file name has quotes: 'filename.zip', instead of filename.zip
I hit a wall, I have no idea even where to start looking, apparently this happens with some mac users. Any ideas?
The HTTP standard would have you putting double quotes around the filename parameter in the Content-Disposition header. That might look like this in your code:
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Type: ' . $mimeType);
header('Content-Type:application/octet-stream');
header('Content-length: ' . $size);
header('Content-Transfer-Encoding: binary');
header('Content-Disposition:attachment;filename="' . $fileName . '"');
readfile($url);
Note here that I have changed all your PHP string definitions to use single quotes to present all string definitions consistently.
I've face weird problem below url when I copy and paste it into download manager, it works fine. But I am going to stream it in direct link via PHP then get an 400 error. However when I use the link into different upload center site (uploadboy.com) to test if the link works fine or not, it was working fine on the uploader.
I coded a script that I attach to affix. And try many different way to download it but still face error 400!
What action do I have taken to fix the problem?
I test to see if the script works urlencode and urldecode function on whole parts of url and on query string of url. in these 4 different ways no success to download. However I attached my script in one of the way I tested.
I notice you again. the link working fine and it's not limited to IP Address. However Download Manager that I test to download the url have same IP address with php webserver.
Sample of URL:
http://r5---sn-p5qlsnsk.googlevideo.com/videoplayback?ipbits=0&mm=31&fexp=9407724,9408142,9408592,9408705,9408710,9408982,9412517,9412776,9413317,9413355,945137,948124,952612,952637,952640,952642&id=o-ACIY_VMiJqYl9lskpYea7F_6tP6DNv6WlJattCarpNST&signature=0DF7579F401F8BB481A4511F96ECD95C52FB618B.29AA2A450464D709E3FC3974759459A1EB7066E9&nh=IgpwcjAzLmlhZDA3KgkxMjcuMC4wLjE&key=yt5&mt=1432113172&ip=198.50.235.216&upn=aR3Slhiml7g&expire=1432134981&sparams=dur,id,ip,ipbits,itag,mime,mm,ms,mv,nh,pl,source,upn,expire&mime=video/3gpp&sver=3&pl=23&source=youtube&mv=u&dur=87.817&itag=17&ms=au&signature= '&title='Youtube-API-V3-Drag--Drop-PlayList--Download-videos--Search-Keyword'
Sample of Code to stream:
$url = $_POST['url'];
$referrer = $_POST['referrer'];
if(strstr($url,'googlevideo.com')){
$url_parts = parse_url($url);
$query = $url_parts['query'];
$query_parts = parse_str($query,$queries);
foreach ($queries AS $key=>$value){
$post_url .= $key.'='.urlencode($value).'&';
}
$post_url = rtrim($post_url, '&');
$url2 = 'http://r1---sn-u2oxu-f5f6.googlevideo.com/videoplayback?'.$post_url;
die($url.'<br><br>'.$url2);
$filename = preg_replace('/[^a-zA-Z0-9-_\.]/','', $title).'.'.getExtension($mime);
// $head = array_change_key_case(get_headers($url, TRUE));
// $filesize = $head['content-length'];
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
// header('Content-Length: '.$filesize); // provide file size
header('Connection: close');
// echo getUrl($url,$_SERVER['HTTP_USER_AGENT'],$referrer);
$file = fopen(str_replace('&','&',$url), 'rb');
while (($content = fread($file, 20480)) !== false) { // Read in 2048-byte chunks
echo $content; // or output it somehow else.
flush(); // force output so far
}
fclose($file);
exit();
}
Update1:
I already test below curl function:
function getUrl($url,$agent='',$referrer='',$proxy=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
if(!empty($agent)){
curl_setopt ($ch, CURLOPT_USERAGENT,$agent);
}
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
if(!empty($referrer)){
curl_setopt ($ch, CURLOPT_REFERER, $referrer);
}
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
The 400 error comes from a typo in your url: remove the space character in this part: &signature= '&title='.
Your curl function is ok, but it doesn't work with your url. I don't know why not, but it works with an url like this: https://youtu.be/LxBk5f6mr-A.
Perhaps you can use the tool mentioned in this post:
grabbing youtube video URL from curl or get_video_info
I want to download a pdf file from this link. From the page, select weekly summary, I want download the first pdf.
PDF Weekly summary - Last week: Sorted by insider
I'm using the following code try to download the pdf,
<?php
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
if (file_exists($file))
{
if (FALSE!== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked'); //changed to chunked
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
//header('Content-Length: ' . filesize($file)); //Remove
//Send the content in chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
?>
It seems I can not download using PHP. I can download by myself confirm the popup window. Any suggestions?
I tried curl also, still can not download. The file size is zero.
<?php
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
$path = 'c:\download\output.pdf';
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
?>
Finally I could download the PDF file with PHP curl and CURLOPT_REFERER setting. Below is the code.
<?php
$url = 'https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA';
$path = "/pdf/output.pdf";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.sedi.ca/sedi/SVTReportsAccessController?menukey=15.03.00&locale=en_CA');
$data = curl_exec($ch);
curl_close($ch);
$result = file_put_contents($path, $data);
if (!$result) {
echo "error";
} else {
echo "success";
}
?>
Instead of getting your PHP server to act as a proxy why not just:
<?PHP
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
header("Location: $file");
?>
Version 2
<?PHP
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="filename.pdf"');
readfile($file);
?>
Version 3
curl_setopt($ch, CURLOPT_REFERER, 'https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA');
Add this line to your original code?
Also, as mentioned here:
Download a image from SSL using curl?
for https we should add the following:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
I had a code that worked, here:
if (isset($_GET['file']) && isset($_GET['name']))
{
$file = base64_decode($_REQUEST['file']);
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);
if (preg_match('/Content-Length: (\d+)/', $data, $matches))
{
// Contains file size in bytes
$contentLength = (int)$matches[1];
}
header("Content-type: application/x-file-to-save");
header("Content-Disposition: attachment; filename=".$_REQUEST['name']);
header('Content-Length: ' . $contentLength);
header('Content-Transfer-Encoding: binary');
#readfile($file);
}
And than I changed my server to a differenf host providor, and the code stoped working. When I download from chrome it just keeps loading and loading,
but when I download from a download manager like IDM than it gets the correct name and size, but when I press start it makes an error that says something about not supporting resuming or something...
Again, the code worked when I used a different providor.
It might be errors which are outputted before readfile() use ob_clean();
flush(); to remove any output before readfile
I have this code:
if (isset($_GET['file']) && isset($_GET['name']))
{
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);
if (preg_match('/Content-Length: (\d+)/', $data, $matches))
{
// Contains file size in bytes
$contentLength = (int)$matches[1];
}
header("Content-type: application/x-file-to-save");
header("Content-Disposition: attachment; filename=".$_REQUEST['name']);
header('Content-Length: ' . $contentLength);
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Connection: Keep-Alive');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0', false);
header('Cache-Control: private', false);
readfile($file);
}
The problem is that $file is located on a different server.
How do I enable resuming this download?
Thanks!
Modify your curl to also pull in the body (remove the NOBODY option)
You can then split the returned content ($data) into the header and the body - the split is the first blank line (look for two carriage returns in a row - the stuff above this is the header, anything below is the body). Put them into variables $header and $body.
Run your preg_match on $header.
To output the rest, simply "echo $body", or substr($body, $startpos) for a resumable download.
Resumable downloads: if the file is large, then you might want to cache it locally. (i.e. save "$body" to a local file, and then use readfile on the local file. Then you can handle the resumable download by opening the file (fopen) and going to the right place (fseek) and reading (fread) / echoing (echo) the rest from there. fpassthru should do the same too, once you've found the starting point.
Also, if you're trying to pass off someone else's content as your own, get permission first ;) (otherwise why not simply direct the user to that other URL and save yourself the bandwidth in and out)