【问题标题】:Get info from API/URL从 API/URL 获取信息
【发布时间】:2022-06-13 16:09:26
【问题描述】:

我有网址https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680 输出统计数据。

例如:

[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]

我想获取链接计数的值。

谁能帮忙?

我尝试过:

    <?php
    $content =     file_get_contents("https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680");

$result  = json_decode($content);

print_r( $result->link );
?>

但是没有用。

【问题讨论】:

  • 为什么会被标记为 JavaScript?
  • “没用”是什么意思?它做了什么
  • 直接在我的浏览器中调用该 URL,只会给我回复 [{"Message":"Connection Failed"}] - 你有什么理由相信这会在你的脚本中表现不同吗?
  • 当你想调试东西时,在尝试访问变量的属性之前执行var_export($result)print_r($result)。通常,在您的情况下,该变量是一个数组,并且您访问了一个缺失的属性,因此它产生了一个错误。我在下面给了你完整的答案。

标签: php api curl file-get-contents


【解决方案1】:

将 JSON 放入编辑器中,您会看到它是一个数组,而不是具有 link 属性的对象。这就是您无法直接访问它的原因。您必须遍历这些项目,然后才能访问其中一项的 link 属性。如果您需要按 id 访问链接,正如您在 4 个月后提出的那样,那么只需在一个按 id 索引的数组中创建一个字典,其中只包含您需要的有趣数据。

PHP 代码:

<?php

// The result of the request:
$content = <<<END_OF_STRING
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
END_OF_STRING;

$items = json_decode($content);

echo '$items = ' . var_export($items, true) . "\n\n";

// Create a dictionnary to store each link accessible by id.
$links_by_id = [];

// Loop over all items:
foreach ($items as $i => $item) {
    // Show how to access the current link.
    echo "Link $i = $item->link\n";
    
    // Fill the dictionary.
    $links_by_id[(int)$item->id] = $item->link;
}

// To access the first one:
echo "\nFirst link = " . $items[0]->link . "\n";

// Example of access by id:
// The id seems to be a string. It could probably be "1895" or "zhb34" or whatever.
// (If they are only numbers, we could convert the string to an integer).
$id = "1859";
echo "\nAccess with id $id = " . $links_by_id[$id] . "\n";

在这里测试:https://onlinephp.io/c/e8ab9

另一个重要点:您在提供的 URL 上收到 403 禁止错误。所以通常情况下,您不会获得所需的 JSON。

正如我在下面的评论中所解释的,我认为如果没有具有有效查询参数和/或 cookie 的新 URL,您将无法访问此页面。我想你从某个地方获得了这个 URL,它不再有效。这就是为什么您可能需要使用 cURL 通过会话访问网站以获取 JSON API 的新 URL。使用 Google 查找一些使用会话处理进行 PHP 抓取/爬网的示例。您会发现,根据网站的不同,它可能会变得相当棘手,尤其是在游戏中加入一些 JavaScript 时。

【讨论】:

  • 感谢您的帮助。
  • 但是当我添加 url 时
  • android.rediptv2.com/… 我收到此消息警告:file_get_contents(android.rediptv2.com/…):打开流失败:HTTP 请求失败!第 6 行 /home/vtpioino/public_html/ch.php 中的 HTTP/1.1 403 FORBIDDEN $items = NULL 警告:第 17 行 /home/vtpioino/public_html/ch.php 中的 foreach() 提供的参数无效 第一个链接 =
  • android.rediptv2.com/…'; // 读取 JSON 文件 $json_data = file_get_contents($api_url); // 将 JSON 数据解码为 PHP 数组 $items = json_decode($json_data);回声'$items ='。 var_export($items, true) 。 "\n\n"; // 遍历所有项目: foreach ($items as $i => $item) { echo "Link $i = $item->link\n"; } // 访问第一个: echo "\nFirst link = " . $items[0]->链接。 "\n";
  • @achraf 错误信息很明确:您收到 403 错误,表示您无权访问此 URL,就这么简单!但是当我查看查询字符串时,我看到了 usercodecheck 参数,这让我认为您需要先进行会话。 You'll have to use cURL with a session。首先进行身份验证以获取对网站的访问权限,然后获取要报废的正确 URL,然后使用相同的会话 cookie 获取它。
猜你喜欢
  • 2021-07-01
  • 2021-05-16
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多