问题
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