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.
$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.
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.
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;
Its funny, I had the opposite question when I started rails development