Receiving data from a url (m3u) using php

1

I have the following question:

I have this URL: http://Infinity.quor8.com:8000/get.php?username=wesleybr&password=mxViBCIK05&type=m3u_plus&output=ts

When you access it from the browser, a .m3u file is downloaded so far:)

I need to put this url in an input field and when pressing the button load below some data that is inside this file, for example:

tvg-logo tvg-name and all urls with final .ts

I would like to show this in form list type:

Nome Logo Url

I do not know where to start, in fact I have tried many things that are not worth putting here: / if someone can give a light:)

    
asked by anonymous 17.01.2018 / 14:29

1 answer

1

The easiest way is to use the preg_match_all function with Regex .

Capturing list through cURL

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://Infinity.quor8.com:8000/get.php?username=USERNAME&password=PASSWORD&type=m3u_plus&output=ts");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

echo $response;

Using this code, you will be able to capture all the contents of the file and store it in the $response variable.

It's important that you use options like CURLOPT_COOKIEJAR , CURLOPT_COOKIEFILE , CURLOPT_USERAGENT etc. Some servers block access when there are no certain headers .

Filtering the contents of the file

As I mentioned at the beginning, to filter you will have to use regex .

<?php

preg_match_all('/((?:tvg-name)="(?<name>.*?)".+(?:tvg-logo)="(?<logo>.*?)".+\n(?P<link>https?:?\/\/.+))/', $response, $result, PREG_SET_ORDER);

$data = reset($result);
  

(?:tvg-name)="(?<name>.*?)" - Here it will capture all the content involved by the "(quotation marks) that comes after tvg-name=

  

.+(?:tvg-logo)="(?<logo>.*?)" - The .+ is for it to go through all content up to tvg-logo=" and capture content from the "(quotation marks)

  

.+\n(?P<link>https?:\/\/.+) - Here the .+ is the same thing as the previous step, the difference is in \n which means to scroll through a line break even and to capture the whole line that begins with http or https

  

The ?: at the beginning of the relatives informs not to capture that term. In this expression it is optional.

  

Finally, (?<name>.*?) informs you to capture the entire value and transform it into a group with the name name

This will return you a array with the name , logo and link data. You can now make a foreach and display the data in a table.

Complete Code

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://Infinity.quor8.com:8000/get.php?username=USERNAME&password=PASSWORD&type=m3u_plus&output=ts");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

if (substr($info["http_code"], 0, 2) != 20) {
    die("Could not connect to server. Check username and password");
}

preg_match_all('/(tvg-name="(?<name>.*?)".+tvg-logo="(?<logo>.*?)".+\n(?P<link>https?:\/\/.+))/', $response, $channels, PREG_SET_ORDER);

?>

<table>
    <thead>
        <tr>
            <th>Logo</th>
            <th>Name</th>
            <th>Link</th>
        </tr>
    </thead>

    <tbody>
        <?php foreach($channels as $channel): ?>
        <tr>
            <td><img src="<?php echo $channel["logo"] ?>" height="75" width="75" /></td>
            <td><?php echo $channel["name"] ?></td>
            <td><?php echo str_replace(".ts", ".m3u8", $channel["link"]) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

You can also customize with css :

<style>
    .channel {
        float:left;
    }
</style>

<div id="channels">
<?php foreach($channels as $channel): ?>
    <div class="channel">
        <a href="<?php echo str_replace(".ts", ".m3u8", $channel["link"]) ?>" title="<?php echo $channel["name"] ?>">
            <img src="<?php echo $channel["logo"] ?>" height="75" width="75" alt="<?php echo $channel["name"] ?>" />
        </a>
    </div>
<?php endforeach; ?>
</div>

If you want to replace a value while adding the data to the table, just use preg_replace or str_replace .

    
17.01.2018 / 15:58