how to get content from external web page by php?

我的未来我决定 提交于 2020-01-24 00:53:05

问题


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 : )


回答1:


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.




回答2:


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 :-)




回答3:


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



来源:https://stackoverflow.com/questions/4365011/how-to-get-content-from-external-web-page-by-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!