Home > OS >  Why I can't get file content using file_get_contents for this site in php
Why I can't get file content using file_get_contents for this site in php

Time:05-14

Peace be upon you. I just developed a free PHP tool that extracts YouTube video details of all public videos in json format. It works perfectly with local server and live server. But the problem is if I get the content using the file_get_contents in php with the local server site it works perfectly but if I try to get the content with the live server it don't work.

Screenshot of Result

Script which i used to get content:

<?php require '../3lsp/3l_smart_php.php';?>
<h1>Using Local Server</h1>

<?php
$x = json_decode(file_get_contents('http://localhost/ytinfo/?id=0WO5uUWHz3o'));
echo '<b style="color:red">Title: </b>'.$x->title.'<hr>';
echo '<b style="color:red">Author: </b>'.$x->author.'<hr>';
echo '<b style="color:red">Quality: </b>'.$x->quality.'<hr>';
echo '<b style="color:red">Views: </b>'.$x->views.'<hr>';
echo '<b style="color:red">Length: </b>'.format_time($x->lengthSeconds).'<hr>';
echo '<b style="color:red">Description: </b>'.$x->shortDescription.'<hr>';
?>

<h1>Using Live Server</h1>

<?php
$y = json_decode(file_get_contents('http://ytinfo.ezyro.com/?id=0WO5uUWHz3o'));
echo '<b style="color:red">Title: </b>'.$y->title.'<hr>';
echo '<b style="color:red">Author: </b>'.$y->author.'<hr>';
echo '<b style="color:red">Quality: </b>'.$y->quality.'<hr>';
echo '<b style="color:red">Views: </b>'.$y->views.'<hr>';
echo '<b style="color:red">Length: </b>'.format_time($y->lengthSeconds).'<hr>';
echo '<b style="color:red">Description: </b>'.$y->shortDescription.'<hr>';
?>

How to Solve?

CodePudding user response:

file_get_contents('http://ytinfo.ezyro.com/?id=0WO5uUWHz3o') is returning the following HTML:

<html>

<body>
  <script type="text/javascript" src="/aes.js"></script>
  <script>
    function toNumbers(d) {
      var e = [];
      d.replace(/(..)/g, function(d) {
        e.push(parseInt(d, 16))
      });
      return e
    }

    function toHex() {
      for (var d = [], d = 1 == arguments.length && arguments[0].constructor == Array ? arguments[0] : arguments, e = "", f = 0; f < d.length; f  ) e  = (16 > d[f] ? "0" : "")   d[f].toString(16);
      return e.toLowerCase()
    }
    var a = toNumbers("f655ba9d09a112d4968c63579db590b4"),
      b = toNumbers("98344c2eee86c3994890592585b49f80"),
      c = toNumbers("d72fb33abc28df4430f7b7643e8aeda2");
    document.cookie = "__test="   toHex(slowAES.decrypt(c, 2, a, b))   "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";
    location.href = "http://ytinfo.ezyro.com/?id=0WO5uUWHz3o&i=1";
  </script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body>

</html>

As you can see, it's creating a cookie with an encrypted value, then reloading the page with the additional i=1 parameter. The server is presumably checking for that that cookie value, and only returns the JSON when it finds it.

The value of the cookie should be 49262d78c44eed05b1683afbc9b5364f, you can add that in your file_get_contents().

$context = stream_context_create([
    'http' => [
        'header' => "Cookie: __test=49262d78c44eed05b1683afbc9b5364f\r\n"
    ]
]);
$y = json_decode(file_get_contents('http://ytinfo.ezyro.com/?id=0WO5uUWHz3o&i=1', false, $context));

CodePudding user response:

It will work like this, create a variable with the URL, use the URL as an argument to the file_get_contents() function, then use json_decode() function to decode JSON string to PHP object, which will display the content perfectly. ALSO make sure you enable allow_url_fopen to use the file_get_contents() function if it isn't already active. We can enable it by setting phpini_set("allow_url_fopen", 1) in the php.ini file.

<h1>Using Live Server</h1>

<?php
$url = 'http://ytinfo.ezyro.com/?id=0WO5uUWHz3o';
$json = file_get_contents($url);
$y = json_decode($json);
echo '<b style="color:red">Title: </b>'.$y->title.'<hr>';
echo '<b style="color:red">Author: </b>'.$y->author.'<hr>';
echo '<b style="color:red">Quality: </b>'.$y->quality.'<hr>';
echo '<b style="color:red">Views: </b>'.$y->views.'<hr>';
echo '<b style="color:red">Length: </b>'.format_time($y->lengthSeconds).'<hr>';
echo '<b style="color:red">Description: </b>'.$y->shortDescription.'<hr>';
?>
  • Related