Skip to content

Instantly share code, notes, and snippets.

@caius
Created March 15, 2009 16:35
Show Gist options
  • Save caius/79469 to your computer and use it in GitHub Desktop.
Save caius/79469 to your computer and use it in GitHub Desktop.
<?php
require "HTTParty.php";
class FMyLife extends HTTParty
{
function __construct() {
$this->base_uri = "http://api.betacie.com/view";
$this->default_params = array("key" => "readonly", "language" => "en");
}
public function random()
{
return $this->get("/random");
}
}
$a = new FMyLife;
var_dump( $a->random() );
/*
string(679) "<?xml version="1.0" encoding="UTF-8"?>
<root><active_key>readonly</active_key><items><item id="168595"><author>Sad</author><category>sex</category><date>2009-03-01T10:08:01+01:00</date><agree>14018</agree><deserved>65176</deserved><comments>52</comments><text>Today, I was typing up a love letter on my computer. A sexual love letter. I was in a classroom, I'm the teacher, I'm gay, and my love letter showed up on the tv screen while my 7th grade students were taking a test. It was up on the screen for 15 minutes. FML</text><comments_flag>0</comments_flag></item></items><code>1</code><pubdate>2009-03-15T14:57:37+01:00</pubdate><language>en</language><errors></errors></root>"
*/
?>
<?php
class HTTParty
{
public $base_uri = "";
public $default_params = array();
public function get($uri_path, $params = array())
{
// Build the URL to grab
$uri = $this->build_url($uri_path, $params);
// Grab the URL
$raw_data = file_get_contents($uri);
// todo: filter xml/json here
return $raw_data;
}
private function build_url($path, $params = array())
{
return $this->base_uri . $path . "?" .
http_build_query(array_merge($this->default_params, $params));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment