0

I am trying to display JSON content in a PHP table but its not working. I can't figure out what I should change.

Here is my code:

<html>
<head>
<title>Download</title>
</head>
<body>
<?php
$myData = file_get_contents("https://youtubetoany.com/@api/json/videostreams/VEou0QBeHlk");
$myObject = json_decode($myData);
$myObjectMap = $myObject->vidInfo;
?>
<table>
<thead>
  <tr>
        <td>Url</td>
        <td>Size</td>
        <td>Quality</td>
        <td>Type</td>
  </tr>
</thead>
<tbody>
  <?php foreach($myObjectMap as $key => $item): ?>
    <tr>
      <td><?PHP echo $item->dloadUrl; ?></td>
      <td><?PHP echo $item->rSize; ?></td>
      <td><?PHP echo $item->round; ?></td>
      <td><?PHP echo $item->quality; ?></td>
      <td><?PHP echo $item->ftype; ?></td>
    </tr>
  <?php endforeach; ?>
</tbody>
 </table>

</body>
</html>    

This is what I get in my browser:

Url Size Quality Type

2
  • 1
    That URL returns incorrectly formatted string and is not compatible with JSON. Use Application such as Postman check the content and it throw Syntax error. Postman Commented Oct 6, 2017 at 22:46
  • you could try to cut everything after the (and including) the first <script before json_decoding. But this source seems not to be made to be grabbed... Commented Oct 6, 2017 at 23:06

2 Answers 2

1

Your source has some javascript attached. You need to get rid of that:

$myData = file_get_contents("https://youtubetoany.com/@api/json/videostreams/VEou0QBeHlk");

// get the substring from start til the first occurence of "<script"
$myRealData = substr($myData,0,strpos($myData,"<script"));
$myObject = json_decode($myRealData);

BUT this source doesn't seem to be made to be grabbed. So I won't rely on that source or that it'll stay the way you find it now.

Sign up to request clarification or add additional context in comments.

Comments

1

I just found probably why its not working. The link returns a JavaScript attached to the bottom of the JSON. So here is my solution.

<html>
<head>
<title>Download</title>
</head>
<body>
<?php
$myData = file_get_contents("https://youtubetoany.com/@api/json/videostreams/VEou0QBeHlk");

// This up to the last occurrence of the "}"
$json_block = substr($myData, 0, strripos($myData, "}"));

$myObject = json_decode($json_block);
$myObjectMap = $myObject->vidInfo;
?>
<table>
<thead>
  <tr>
        <td>Url</td>
        <td>Size</td>
        <td>Quality</td>
        <td>Type</td>
  </tr>
</thead>
<tbody>
  <?php foreach($myObjectMap as $key => $item): ?>
    <tr>
      <td><?PHP echo $item->dloadUrl; ?></td>
      <td><?PHP echo $item->rSize; ?></td>
      <td><?PHP echo $item->round; ?></td>
      <td><?PHP echo $item->quality; ?></td>
      <td><?PHP echo $item->ftype; ?></td>
    </tr>
  <?php endforeach; ?>
</tbody>
 </table>

</body>
</html>

4 Comments

there is a } right at the end of the stream: ),0,{}));. So I doubt this will succeed.
better search for the first <script
@Jeff I was going to do that, but in case the original file no longer has script tags, that might throw errors. This in much safer solution. Plus strripos use the last occurrence on the file. Only problem would be if there&amp;#39;s a } with in the script. In that case your way is better or regex might come in to play
any version is unrelyable with this source, you're right... The chance to have a } in javascript is quite high though...