Playing .m3u8 video using php curl - php

I'm attempting to play a .m3u8 video using a php curl proxy. The following code seems to work, although it only returns the video #EXTM3U information but does not play the video.
Code:
<?php
//....proxy info
$auth = 'username:password';
$proxy_ip = '1.2.3.4.5';
$proxy_port = 8080;
$path = $_GET['link'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $auth);
curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
echo $error_msg;
}
curl_close($ch);
if (isset($error_msg)) {
echo $error_msg;
}
?>
Output:
#EXTM3U #EXT-X-VERSION:3 #EXT-X-INDEPENDENT-SEGMENTS #EXT-X-STREAM-INF:BANDWIDTH=5640800,AVERAGE-BANDWIDTH=5640800,CODECS="avc1.4d4028,mp4a.40.2",RESOLUTION=1920x1080,FRAME-RATE=25.000 index4147.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=2421100,AVERAGE-BANDWIDTH=2421100,CODECS="avc1.4d401f,mp4a.40.2",RESOLUTION=1280x720,FRAME-RATE=25.000 index2073.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=1566400,AVERAGE-BANDWIDTH=1566400,CODECS="avc1.4d401f,mp4a.40.2",RESOLUTION=960x540,FRAME-RATE=25.000 index1296.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=1002100,AVERAGE-BANDWIDTH=1002100,CODECS="avc1.77.30,mp4a.40.2",RESOLUTION=746x420,FRAME-RATE=25.000 index783.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=774400,AVERAGE-BANDWIDTH=774400,CODECS="avc1.77.30,mp4a.40.2",RESOLUTION=640x360,FRAME-RATE=25.000 index576.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=421300,AVERAGE-BANDWIDTH=421300,CODECS="avc1.42c015,mp4a.40.2",RESOLUTION=426x240,FRAME-RATE=25.000 index255.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=476300,AVERAGE-BANDWIDTH=476300,CODECS="avc1.42c01f,mp4a.40.2",RESOLUTION=640x360,FRAME-RATE=25.000 index101.m3u8
Any ideas regarding how I can play the video?

Facing the same issue...
Had any luck figuring it out?
I was also facing the same issue, until just a few moments ago.
Was able to generate a m3u8 file in php and set the mime type correctly but videojs and vlc just wouldn't play it...
If I downloaded the output, it would be good for vlc or videojs.
Followed a tip from Markus AO on How to create dynamic m3u8 using php and it worked.
Basically, it should be ignoring the mime type (although I haven't tried removing it) and just looks at the extension.
Just used the htaccess to make it so that when they call some m3u8's it actually calls my php file
Added this to my .htaccess:
RewriteRule ^proxy([^.]+).m3u8$ proxy.php
I'm actually sending the address as a GET parameter such as your $_GET['link']
The wildcard in front of proxy and before the .m3u8 extension is just because I've noticed that a plugin that I've installed in chrome would probably ignore that the parameter changed and just show the same stream as long as it was the proxy.m3u8 file.
Hoping this can be helpful to anybody

Related

How Do I Download Facebook Images Using cURL?

I have a PHP script that works just fine downloading most remote images to my file system, but when I try to download a Facebook or Instagram image I get an error that says "failed to open stream: No error in" followed by the line of my fopen function and two additional errors "fwrite() expects parameter 1 to be resource, bool given in" which is obviously due to the Facebook image not downloading.
My code is as follows:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER,1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_REFERER, "https://www.facebook.com/");
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
$page = curl_exec($curl);
if(curl_errno($curl)):
echo 'Erro: ' . curl_error($curl);
exit;
endif;
curl_close($curl);
// Use basename() function to return the base name of file
$file_name = basename($url);
if(file_exists($file_name)){
unlink($file_name);
}
$fp = fopen($file_name,'x');
fwrite($fp, $page);
fclose($fp);```
There also seems to be an error when I try to add the first line of code to this post which is:
```$url = 'https://scontent-sea1-1.xx.fbcdn.net/v/t1.0-0/p180x540/119041795_176402070632464_6192328410277888324_o.jpg?_nc_cat=111&ccb=2&_nc_sid=825194&_nc_ohc=O7khk9mGFO4AX86nd5X&_nc_ht=scontent-sea1-1.xx&tp=6&oh=ff35f5eaf960fa7bd30ab1d549f0d817&oe=6045A0D1';```
Screenshot APIs are the workaround for this. There are many, but to spare the one I am using from any scrutiny for how it is being used by now I will not name the specific program. Most are basically the same, but you should choose one that allows you to install it on your own system rather than being dependent on simply getting a screenshot from a URL of theirs. That way you can encode the Facebook/Instagram image URL that you want to take a screenshot of, send the encoded URL to the API, and save the result on your file system.
The ability to encode the URL is key because of all the query strings in Facebook/Instagram image URLs.

Download a file from Dropbox with cURL

i would like to get some help about the following problem. I'm under windows and i'm able to download any files using the cURL, but when it comes to download from Dropbox i'm unable to do it. Even if i use ?raw=1 or ?dl=1 which is responsible to redirect me to the file i still can't do it.
Here is the script i'm using:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'any url?raw=1');
$fp = fopen('backup.wpress', 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);
Thanks in advance. I would be very grateful for any suggestions and help.
There's a 302 redirect on that URL, so you'll need to add the line
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
(You also might want to edit that URL out the post, not sure if it's sensitive data...)

Save mp3 file from a download link on your hosting space using PHP

I am fetching data from API of a service provider (Say- http://serviceprovider.com).
From several parameter one is MP3 download Link (example- http://serviceprovider.com/storage/read?uid=475b68f2-a31b-40f8-8dfc-5af791a4d5fa_1_r.mp3&ip=255.255.255.255&dir=recording)
When I put this download link on my browser it saves it to my local PC.
Now My Problem -
I want to save this MP3 file in one of folder on my hosting space, from where I can further use it for playing using JPlayer Audio.
I have tried file_get_contents(), but nothing happened.
Thanks in advance.
Edit:
After reading Ali Answer I tried the following code, But still not working fully.
// Open a file, to which contents should be written to.
$fp = fopen("downloadk.mp3", "w");
$url = 'http://serviceprovider.com/storage/read?uid=475b68f2-a31b-40f8-8dfc-5af791a4d5fa_1_r.mp3&ip=255.255.255.255&dir=recording';
$handle = curl_init($url);
// Tell cURL to write contents to the file.
curl_setopt($handle, CURLOPT_FILE, $fp);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HEADER, false);
// Do the request.
$data = curl_exec($handle);
// Clean up.
curl_close($handle);
fwrite($fp, $data);
fclose($fp);
This created the file download.mp3 file on my server but with 0 bytes, i.e. empty.
The url used here is a download link example not a mp3 file that can be played with modern browser directly.
Function file_get_contents is used for reading local files. What you have is an URL and in order to fetch the contents, you need to do a HTTP request in your script. PHP comes with the curl extension, which provides you with a stable library of functions for doing HTTP requests:
http://php.net/manual/en/book.curl.php
Using curl to download your file could be done like this:
// Open a file, to which contents should be written to.
$downloadFile = fopen("download.mp3", "w");
$url = "http://serviceprovider.com/storage/read?uid=475b68f2-a31b-40f8-8dfc-5af791a4d5fa_1_r.mp3&ip=255.255.255.255&dir=recording";
$handle = curl_init($url);
// Tell cURL to write contents to the file.
curl_setopt($handle, CURLOPT_FILE, $downloadFile);
// Follow redirects.
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
// Do the request.
curl_exec($handle);
// Clean up.
curl_close($handle);
fclose($downloadFile);
You should probably add some error checking.

Saving Google Chart image to file using PHP

I have a script which generates Google Chart images for clients. It works fine displaying in HTML but I now need to save each chart locally as a .png.
If I try:
$imageData = file_get_contents($url);
file_put_contents('C:/xampp/htdocs/image.png',$imageData);
then I always receive
[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400
The script works fine for normal images, but not Google charts. Same result if I use cURL to save the image too.
An example of my Google chart $url is below. I've hexed out the colons and pipe symbols but it did not fix the issue:
http://chart.apis.google.com/chart?cht=lc&chxt=x,y&chs=700x400&chco=76A4FB,FF9900&chg=0,8.3333333&chdl=Visits|Unique%20visits&chls=3|3&chma=40,20,20,30&chxr=1,0,33411&chds=0,33411&chd=t%3A33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411|33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411&chxl=0%3A|Jan-10|Feb-10|Mar-10|Apr-10|May-10|Jun-10|Jul-10|Aug-10|Sep-10|Oct-10|Nov-10|Dec-10
CURL example:
<?php
$ch = curl_init("www.example.com/curl.php?option=test");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output; ?>
I managed to get this to work in the end - it seems the spaces in the label names were causing errors, replacing with %20 did the trick

PHP rel=NOFOLLOW

I want to download files, from an website using PHP.
And i want to create an php script to download files without going on their website to download files. I just want to pun their link on my script an download the file automatically.
I try with CURL, but doesn't work.... The link is like this <a rel="nofollow" href="/download-15866-114621.srt"><b>Download</b></a>
the code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'http://subtitrari.regielive.ro/download-15866-114621.srt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
echo $contents;
curl_close ($ch);
I get "download failed!" as content, which means they probably have some sort of download protection. The best thing is probably to ask them what you should do (assuming you have their permission to download the file) or stop trying (assuming you don't).
Eitherway, try setting a referer header with CURLOPT_REFERER. Maybe they check that header to see that no-one is hotlinked to the file.

Categories