views:

77

answers:

4

I know how to do this in Ruby, but I want to do this in PHP. Grab a page and be able to parse stuff out of it.

+1  A: 
$file = file_get_contents("http://google.com/");

How to parse it depends on what you are trying to do, but I'd recommend one of the XML libraries for PHP.

Matthew Scharley
This will only work if allow_url_fopen is set to true in your php.ini file. Otherwise, you'll have to change the setting (or if you don't have access to it, then cURL is your only other option).
BraedenP
+3  A: 

Take a look at cURL. Knowing about cURL and how to use it will help in many ways as it's not specific to PHP. If you want something specific however, you can use file_get_contents which is the recommended way in PHP to get the contents of a file into a string.

Bartek
A: 

You could use fopen in read mode: fopen($url, 'r'); or more simply file_get_contents($url);. You could also use readfile(), but file_get_contents() is potentially more efficient and therefore recommended.

Note: these are dependent on config (see the linked manual page) but will work on most setups.

For parsing, simplexml is enabled by default in PHP.

$xmlObject = simplexml_load_string($string);
// If the string was valid, you now have a fully functional xml object.

echo $xmlObject->username;
Benji XVI
Thanks for the simplexml bit, that will be helpful.
Josh K
A: 

Its funny, I had the opposite question when I started rails development

Tyler Gillies
Yeah, I worked that out when I was doing automated google searches (googleplainly.com). Year later and I still haven't linked everything up to automatically add results to the database. But the ruby fetching work fine.
Josh K