Skip to content

Instantly share code, notes, and snippets.

@speedmax
Created January 12, 2009 22:52
Show Gist options
  • Save speedmax/46210 to your computer and use it in GitHub Desktop.
Save speedmax/46210 to your computer and use it in GitHub Desktop.
<?php
function openbet($class, $attrs = array()) {
$class = "Openbet_".ucwords($class);
return new $class;
}
class Openbet_Object {
function __construct($attrs = array()) {
# set object property
foreach($attrs as $key => $value) {
if (isset($this->map[$key]))
$key = $this->map[$key];
if ($key === false)
continue;
$this->$key = (string)$value;
}
}
function __toString() {
return $this->title;
}
}
class Openbet_Category extends Openbet_Object {
var $map = array(
'category' => 'title',
'display_order' => false
);
}
class Openbet_Class extends Openbet_Object {
var $map = array(
'description' => 'title',
'display_order' => false,
'sort' => false,
'category' => false,
'class_id' => false,
'fixed_odds_available' => false
);
}
class Openbet_Type extends Openbet_Object {
var $map = array(
'description' => 'title',
'display_order' => false,
'fixed_odds_available' => false
);
}
class OxiRequest {
function __construct($site = 'https://prod_test:[email protected]/oxi_prop') {
$parts = parse_url($site);
if (isset($parts['user']) && isset($parts['pass'])) {
$this->settings = array(
'user' => $parts['user'],
'pass' => $parts['pass']
);
}
$this->http = new WebModel;
$this->settings['url'] = "{$parts['scheme']}://{$parts['host']}{$parts['path']}";
}
function request($request) {
extract($this->settings);
$request = <<<EOD
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE opsRequest SYSTEM "opsRequest.dtd">
<opsRequest version="1.0">
<reqClientAuth user="$user" password="$pass"/>
$request
</opsRequest>
EOD;
$response = $this->http->post($url, array('xml'=>$request));
return new SimpleXmlElement($response);
}
function getCategories() {
$rs = $this->request('<Level level="Category"/>');
return $rs->DDList->Category;
}
function getClasses($id) {
$id = (string) $id;
$rs = $this->request("<DrillDown level='Category'><Id id='$id'/></DrillDown>");
return $rs->DDList->Class;
}
function getTypes($id) {
$id = (string) $id;
$rs = $this->request("<DrillDown level='Class'><Id id='$id'/></DrillDown>");
return $rs->DDList->Type;
}
function getHeirachy() {
$results = $classes = $types = array();
foreach ($this->getCategories() as $cat) {
foreach ($this->getClasses($cat['category']) as $class) {
if ($class['status'] != 'A') {
continue;
}
foreach ($this->getTypes($class['class_id']) as $type) {
if ($type['status'] != 'A') {
continue;
}
$tmp = $types[] = new Openbet_Type($type->attributes());
}
$class = new Openbet_Class($class->attributes());
$class->types = $types;
$classes[] = $class;
}
$category = new Openbet_Category($cat->attributes());
$category->classes = $classes;
$results[] = $category;
}
return $results;
}
}
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;
}
$query_string = $this->_toUrlQuery($vars);
/* Default */
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout);
/* Request Methods */
if($this->method == "POST"){
curl_setopt($this->curl, CURLOPT_POST, true);
if (array_key_exists('xml', $vars)) {
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $vars['xml']);
} else {
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $query_string);
}
}
else if(!empty($query_string)){
$url = $url.'?'.$query_string;
}
/* 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