0

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.

Josh K
  • 28,364
  • 20
  • 86
  • 132

4 Answers4

4

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
  • 15,269
  • 2
  • 58
  • 65
1
$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
  • 127,823
  • 52
  • 194
  • 222
  • 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 Nov 02 '09 at 02:42
0

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
  • 2,192
  • 1
  • 25
  • 24
0

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

Tyler Gillies
  • 1,857
  • 4
  • 22
  • 31
  • 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 Nov 02 '09 at 12:53