Simon
Simon

Reputation: 17

how to get content from external web page by php?

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

Upvotes: 0

Views: 9502

Answers (3)

Luca Matteis
Luca Matteis

Reputation: 29267

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.

Upvotes: 1

Xtophe
Xtophe

Reputation: 2277

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

Upvotes: 0

Rob
Rob

Reputation: 10258

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

Upvotes: 0

Related Questions