user8734875
user8734875

Reputation:

Display JSON info in a PHP table

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

Upvotes: 0

Views: 68

Answers (2)

Jeff
Jeff

Reputation: 6953

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.

Upvotes: 1

Prav
Prav

Reputation: 2894

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>

Upvotes: 1

Related Questions