0

When I go to this link https://example.com/ch.php?content_id=1&stream_title=HD I get the content_stream of SD, I want to choose a specific stream_title (SD or HD or FHD)

Can anyone help me, please?

<?php
// The result of the request:
$json = <<<END_OF_STRING
{"live":  [{"content_id": "1","title": "Channel 1","country_id": "2","language_id": "6","genre_id": "1","image_url": "","epg": {"daily": "","weekly": ""},"streams": {"live": [{"stream_title": "SD","content_stream": "http:\/\/example.com\/sd\/1","default": "0"}, {"stream_title": "HD","content_stream": "http:\/\/example.com\/hd\/1","default": "1"}, {"stream_title": "FHD","content_stream": "http:\/\/example.com\/fhd\/1","default": "0"}],"dvr": ""}}, {"content_id": "2","title": "Channel 2","country_id": "2","language_id": "6","genre_id": "2","image_url": "","epg": {"daily": "","weekly": ""},"streams": {"live": [{"stream_title": "SD","content_stream": "http:\/\/example.com\/sd\/2","default": "0"}, {"stream_title": "HD","content_stream": "http:\/\/example.com\/hd\/2","default": "1"}, {"stream_title": "FHD","content_stream": "http:\/\/example.com\/fhd\/2","default": "0"}],"dvr": ""}}]}
END_OF_STRING;

$json_decoded = json_decode($json);

$content_id = $_GET['content_id'];
$stream_title = $_GET['stream_title'];
$desiredLink = '';



for ($i = 0; $i < count($json_decoded->{'live'}); $i++) {
    if ($content_id == $json_decoded->{'live'}[$i]->{'content_id'}) {
        $desiredLink = $json_decoded->{'live'}[$i]->{'streams'}->{'live'}[$i]->{'content_stream'};
    }
}

echo $desiredLink;


achraf
  • 15
  • 5
  • 1
    It's unclear where you're stuck with this? And how should it work in practice? Do you mean the required type of stream should be specified via another GET parameter? – ADyson Oct 24 '22 at 21:56
  • @ADyson Yes I want the required type of stream should be specified via another GET parameter – achraf Oct 24 '22 at 21:59
  • ok. So where are you having a problem? Sure you just replace `->{'live'}[$i]->{'content_stream'}` with `->{'live'}[$_GET['stream_type']->{'content_stream'}` ...something like that? – ADyson Oct 24 '22 at 22:05
  • @ADyson I try it but didn't work and stream_title not stream_type – achraf Oct 24 '22 at 22:24
  • Ah sorry I just noticed the title is a property of the object, not an index. You need to search inside the "live" array then. See https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value for how to do that. – ADyson Oct 24 '22 at 22:26
  • @ADyson Can you please do it for me – achraf Oct 24 '22 at 22:30
  • Why? Where are you stuck? I just showed you an explanation already of how to find the correct object within the array. Did you read the linked article? Did you try anything? What went wrong? Please show your attempt and explain the error/problem – ADyson Oct 24 '22 at 22:38
  • @ADyson I tried it and the result appears a white page – achraf Oct 24 '22 at 22:43
  • If your PHP code crashed and you didn't enable error reporting, then you might get a white page. Hopefully that means PHP is set to log errors to a file, and you can go and check the error log file to find out what the problem is. If not, you'll need to enable PHP error reporting (it's easy to google how to do this). But if you show me the code you used, I might be able to spot a mistake as well. – ADyson Oct 24 '22 at 22:45

0 Answers0