首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从API/URL获取信息

从API/URL获取信息
EN

Stack Overflow用户
提问于 2022-02-01 09:08:34
回答 1查看 249关注 0票数 -2

我有一个输出统计信息的URL 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

例如:

代码语言:javascript
运行
复制
[{"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"}]

我想得到链接计数的值。

有人能帮忙吗?

我试着做:

代码语言:javascript
运行
复制
    <?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 );
?>

但没起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-01 09:35:47

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

PHP代码:

代码语言:javascript
运行
复制
<?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

另一点很重要:你得到了你提供的网址上的一个403个禁止的错误。因此,通常情况下,您将无法获得所需的JSON。

正如我在下面的注释中解释的那样,我认为如果没有一个具有有效查询参数和/或cookie的新URL,您就无法访问这个页面。我想您从某个地方获得了这个URL,并且它不再有效。这就是为什么您可能需要使用 cURL 来访问带有会话的网站,以获得JSON 的新URL。使用Google可以找到PHP抓取/爬行会话处理的一些示例。你会看到,取决于网站,它可能会变得相当棘手,特别是当一些JavaScript进入游戏。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70937944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档