如何通过php从外部网页获取内容?

发布于 2024-10-06 09:11:09 字数 252 浏览 0 评论 0原文

我想获取网页中的“标题”、“描述”和“关键字” 我知道 3 种方法来实现这项工作:

a) 使用 CURL

b) 使用 fopen

c) 使用 get_meta_data()

奇怪的是,上面的每一种方法并不是每次都能正确工作。

对于相同的网址:

有时,我可以获得内容。

有时,它会返回错误:“无法打开流:HTTP 请求失败”

我很困惑。为什么? 帮我 : )

I want get 'title'、'description'and'keywords' in a web page
I know 3 ways to implement this job:

a) use CURL

b) use fopen

c) use get_meta_data()

Strangely,each of the above does not work correctly every time.

for the same url:

Sometimes,I can get the content.

Sometimes,it return an error:'failed to open stream: HTTP request failed'

I'm confused.WHY?
help me : )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味超标? 2024-10-13 09:11:09

您可以使用 file_get_contents("http://someurl.com");< /a> 获取外部网站。

结果将是一个包含网页的整个 HTML 的字符串。然后,您可以使用 PHP 的 HTML 解析器解析该 HTML,以获得所需的信息。

编辑 正如 El Yobo 所指出的,可以禁用此功能。要启用它,您需要启用 fopen 包装器< /a>.

You can use file_get_contents("http://someurl.com"); to fetch an external website.

The result will be a string containing the entire HTML of the webpage. You can then parse that HTML using an HTML parser for PHP to get the information you need.

EDIT as noted by El Yobo, this feature can be disabled. To enable it you need to enable the fopen wrappers.

明媚如初 2024-10-13 09:11:09

可能值得尝试找出错误代码是什么?您是否尝试在第 3 方服务器上执行 HttpRequest - 可能是他们限制您的访问,也可能是任何其他原因,包括合法超时,因为您的互联网连接不好,因此可能值得发布更多数据:-)

might be worth trying to find out what the error code is? are you trying to perform an HttpRequest on a 3rd party server - it could be that they are restricting you access, it could also be any number of other things including legitimate time-outs because your internet connection is no good so it might be worth posting some more data :-)

北凤男飞 2024-10-13 09:11:09

“有时”让我认为您的服务器与网络的连接可能存在问题。您是否尝试过直接从服务器浏览页面,例如使用 curlwget

否则,我通常按照 Luca 的建议实现 file_get_contents 。
http://www.php.net/manual/en/function .file-get-contents.php

The "Sometimes" lets me think that there might be issues in your connection to the network ffrom your server. Have you tried to browse the page directly from the server, for instance using curl or wget?

Otherwise, I usually implement file_get_contents as advised by Luca.
http://www.php.net/manual/en/function.file-get-contents.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文