gpt4 book ai didi

php - file_get_contents 对 'https://en.wikipedia.org/wiki/Category:Upcoming_singles' 的变量响应

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:03:46 31 4
gpt4 key购买 nike

file_get_contents('https://en.wikipedia.org/wiki/Category:Upcoming_singles');  

使用 Chrome 网络浏览器访问同一地址(显示 4 种产品)返回不同的响应(2 种产品)。

经检查,我怀疑这可能与

有关

Saved in parser cache key with ... timestamp ...

在返回的 html 中。当我使用 file_get_contents()

时,时间戳较旧

关于如何使用 file_get_contents() 获取最新信息有什么想法吗?

谢谢!

最佳答案

假设 file_get_contents 正在发出 http 请求,最好检查指定的用户代理。

我听说使用某些用户代理获取数据时会出现问题。看看this question .

您可以使用流上下文指定其他选项(包括用户代理):

<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);

查看 file_get_contents docs .

此外,正如 Jack 所说,cURL 是更好的选择。

编辑:

你误会我的意思了。您要添加的是一个不同的用户代理。例如,使用 mozilla firefox 的用户代理可以获得 4 个结果:

<?php

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; es-AR; rv:1.9.2.23) Gecko/20110921 Ubuntu/10.10 (maverick) Firefox/3.6.23"
)
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://en.wikipedia.org/wiki/Category:Upcoming_singles', false, $context);
print $file;

但是,我认为这不“合法”,作弊可不好。我认为维基百科必须提供任何其他用户代理来从外部应用程序获取其数据。

关于php - file_get_contents 对 'https://en.wikipedia.org/wiki/Category:Upcoming_singles' 的变量响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7681368/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com