如何通过php从外部网页获取内容? [英] how to get content from external web page by php?

查看:40
本文介绍了如何通过php从外部网页获取内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在网页中获取'title'、'description'和'keywords'我知道3种方法来实现这个工作:

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

a) 使用 CURL

b) 使用 fopen

b) use fopen

c) 使用 get_meta_data()

c) use get_meta_data()

奇怪的是,上面的每一个都不能每次都正常工作.

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

对于相同的网址:

有时,我可以获取内容.

Sometimes,I can get the content.

有时会返回错误:'failed to open stream: HTTP request failed'

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

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

I'm confused.WHY? help me : )

推荐答案

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

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

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

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 正如 El Yobo 所指出的,可以禁用此功能.要启用它,您需要启用 fopen 包装器.

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

这篇关于如何通过php从外部网页获取内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆