Skip to content

Instantly share code, notes, and snippets.

@speedmax
Created November 20, 2008 22:55
Show Gist options
  • Save speedmax/27258 to your computer and use it in GitHub Desktop.
Save speedmax/27258 to your computer and use it in GitHub Desktop.
<?php
h2o::addTag('load_sports_events');
class Load_sports_events_Tag extends H2o_Node {
function render($context, $stream) {
$cache = h2o_cache($context->options);
$cache_key = 'digg_xml_feed';
# try cached
if (! ($feed = $cache->read($cache_key))) {
$feed = file_get_contents('http://feeds.digg.com/digg/popular.rss');
$cache->write($cache_key, $feed);
}
$feed = new SimpleXMLElement($feed);
$feed = $feed->xpath('//channel/item');
$context->set('sports_events', $feed);
}
}
?>
<?php
/*
* Class: WebModel
* defines features to request/post web content including getting content of a static/dynamic
* web location. It also allows secure connection, post/get method, http authentication.
*
* Dependencies:
* PHP 4+ with CURL extension enabled.
*
* Author:
* Taylor Luk
*
* Example:
* (code)
* // Simple get operation from a web resource
* $http = new WebModel;
* $http->get("http://domain.com");
*
* // Post data to a web address
* $http = new WebModel;
* $people = $http->post("http://people.com/search.php", array(
* "name" => "tom",
* "age" => "26"
* ))
*
* // Request/Post/Retrive information from a restricted web location where
* // secure connection and authentication is requried.
* $http = new WebModel;
* $http->autenticate = 'username:password';
* $businessAddr = $http->post("http://address/to/secure/location", array(
* "APIKey" => "2Asdf34QW234ASDFASDFXc",
* "location" => "sydney"
* ));
* (end)
*
*/
class WebModel {
var $timeout = 30;
var $curl = null;
var $authenticate = false;
var $method = "GET";
var $response;
var $info;
var $error;
function WebModel(){
}
/* Function: get
* perform a http get request against a url with supplied parameters.
*
* Arguments:
* $url - web location to perform get request.
* $vars - array additional post parameters in key and value pair.
*/
function get($url, $vars=array()){
return $this->request('GET', $url, $vars);
}
/* Function: post
* perform a http post request against a url with supplied parameters,
* if post content is empty
* then stop.
*
* Arguments:
* $url - web location to perform post request.
* $vars - array additional post parameters in key and value pair.
*/
function post($url, $vars){
if(!empty($vars)){
return $this->request('POST', $url, $vars);
} else{
return false;
}
}
/* Function: request
* perform a http post request against a url with supplied parameters,
* if post content is empty
* then stop.
*
* Arguments:
* $url - web location to perform post request.
* $vars - array additional post parameters in key and value pair.
*/
function request( $method = null, $url, $vars = array()){
$this->curl = curl_init();
if(! $this->curl){
return false;
}
if($method){
$this->method = $method;
}
$vars = $this->_toUrlQuery($vars);
/* Default */
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($this->curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($this->curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
/* Request Methods */
if($this->method == "POST"){
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $vars);
}
else if(!empty($vars)){
$url = $url.'?'.$vars;
}
/* Request URL */
curl_setopt($this->curl, CURLOPT_URL, $url);
/* Authenticate */
if($this->authenticate){
$this->authentication();
}
/* Execute Request */
$this->response = curl_exec($this->curl);
$error = $this->detectHttpError();
/* Finalise */
curl_close($this->curl);
if($error){
return false;
}
return $this->response;
}
function detectHttpError(){
$this->info = curl_getinfo($this->curl);
/* error detection */
if(!$this->response){
$this->error = curl_error($this->curl);
return true;
}
else if((int)trim($this->info['http_code']) >= 400){
$this->error = "Http ".$this->info['http_code']." Error";
return true;
}
return false;
}
/* Function: authentication
* if user supplies credential information then perform http authentication against
* desired web location.
*/
function authentication(){
$credential = explode(":", $this->authenticate);
if(sizeof($credential) == 0) {
return false;
}
$user = $credential[0];
$pass = @$credential[1];
curl_setopt($this->curl, CURLOPT_USERPWD, "$user:$pass");
return true;
}
/* Function: _toUrlQuery
* private function to transform array of query into url query.
*/
function _toUrlQuery($qarray){
$query = array();
foreach($qarray as $key => $value){
array_push($query, $key."=". urlencode($value));
}
return join("&", $query);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment