Kohana v3

Alternative to file_get_contents() – Kohana v3

Few days ago, I have published the web application FF2FB – an app that will import your Friendfeed feeds and likes to Facebook. Although it is far from completion, I have managed to add minor features like importing feeds from Friendfeed and posting it on that site (at the home page).

The first version I used to import the feeds was file_get_contents(). It works locally. However, it takes me a day to figure it out, thanks to Kohana logs. I logged the import events so that when the import fails, I will know what caused it. What I discovered was that file_get_contents() on a remote url is not allowed because allow_url_fopen is disabled on my web host.

Looking for alternatives, I looked on my WordPress blog codes to check if how they do the same thing. Although some parts still uses file_get_contents(), others uses CURL. I checked the web host’s phpinfo and found out that CURL was enabled. I did not waste time and immediately dig Kohana’s system directory looking for something to that uses CURL and the like.

There is Kohana Remote. It is actually a helper class that can talk to a remove URL via CURL.

$response = Remote::get($url);

The class that I’m working is that it will get a Friendfeed user’s feeds and likes from Friendfeed API. It is encoded in JSON so I have to decode it when receiving a response. Here the the code snippet.

	 * Retrieves feed from a url
	 * @param string $url
	 * @return array
	public function get($url)
		try {
			$data = Remote::get($url);
		catch (Exception $e)
				$e->getMessage() . ": Error while getting feeds at $url on " . __CLASS__ . " on function " . __FUNCTION__ . " line " . __LINE__
			return FALSE;
		if ($data)
			$data = json_decode($data);
			if ($data instanceof stdClass && !empty($data->entries))
				return self::parse_entries($data->entries);
		return FALSE;

I used CURL to import feeds and you know what? I also used CURL as part of cron job that will trigger the importation of feeds. That CURL cron job is installed on a separate server since my host does not provide cron jobs.

Update: I now used a web-based cron via www.setcronjob.com.

Leave a reply

Your email address will not be published. Required fields are marked *