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]; }