Ben
Ben is a lifelong Nintendo fan who likes to build websites, and make video games. He buys way too much Lego.
WordPress and Games
In my post about cURL the other day Matt mentioned that file_get_contents is a simpler method to use, and in many ways he’s right.
file_get_contents is a PHP functions that gets the contents of a file. Unlike cURL it doesn’t require any configuration so you can simply call it as so:
$data = file_get_contents('http://url_to/get_contents/from');
There are some disadvantages, a big one being that some webhosts block the function, but for really quick data retrieval this function is perfect.
Using file_get_contents the example code from my cURL demo would become:
<?php function bm_getWeather ($code = '', $temp = 'c') { $file = 'http://weather.yahooapis.com/forecastrss?p=' . $code . '&u=' . $temp; $result = file_get_contents($file); $output = array ( 'temperature' => bm_getWeatherProperties('temp', $result), 'weather' => bm_getWeatherProperties('text', $result), 'weather_code' => bm_getWeatherProperties('code', $result), 'class' => 'weatherIcon-' . bm_getWeatherProperties('code', $result), ); return $output; } function bm_getWeatherProperties ($needle, $data) { $regex = ''; preg_match($regex, $data, $matches); return $matches[1]; }
Yeah, file_get_contents is simpler to get remote content, but not all hosts support it. It requires allow_url_fopen is turned on. So, to make sure it works, you should check it before using:
if (ini_get(‘allow_url_fopen’)) {
// use file_get_contents
} else {
// use cURL
}
nice tip. I didn’t know that was needed to be able to use the command. Thanks for the pointer.
nice..
but how can I get a specific url from another page ?
the url is changing, so I want to use file_get_contents.
For emample:
how can I track the video url form this page:
http://www.an-tv.com/watch_video.php?v=9RMXGYA3451G
tracking the url changes to my site ?
please help
$needle is not used.