2007-08-30 02:12:23 +02:00
|
|
|
<?php
|
|
|
|
|
2008-01-08 07:37:50 +01:00
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage integration
|
|
|
|
*/
|
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
/**
|
|
|
|
* RestfulService class allows you to consume various RESTful APIs.
|
|
|
|
* Through this you could connect and aggregate data of various web services.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage integration
|
2007-08-30 02:12:23 +02:00
|
|
|
*/
|
|
|
|
class RestfulService extends ViewableData {
|
|
|
|
protected $baseURL;
|
|
|
|
protected $queryString;
|
|
|
|
protected $rawXML;
|
|
|
|
protected $errorTag;
|
2007-11-15 23:40:36 +01:00
|
|
|
protected $checkErrors;
|
|
|
|
protected $cache_expire;
|
2007-08-30 02:12:23 +02:00
|
|
|
|
2007-11-15 23:40:36 +01:00
|
|
|
function __construct($base, $expiry=3600){
|
2007-08-30 02:12:23 +02:00
|
|
|
$this->baseURL = $base;
|
2007-11-15 23:40:36 +01:00
|
|
|
$this->cache_expire = $expiry;
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Query string parameters to send a request.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @param $params array An array passed with necessary parameters.
|
2007-08-30 02:12:23 +02:00
|
|
|
*/
|
|
|
|
function setQueryString($params=NULL){
|
|
|
|
$this->queryString = http_build_query($params,'','&');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function constructURL(){
|
|
|
|
return "$this->baseURL?$this->queryString";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects to the RESTful service and gets its response.
|
|
|
|
* TODO implement authentication via cURL for
|
|
|
|
*/
|
|
|
|
|
|
|
|
function connect(){
|
2007-11-15 23:40:36 +01:00
|
|
|
$url = $this->constructURL(); // url for the request
|
|
|
|
|
|
|
|
// check for file exists in cache
|
|
|
|
// set the cache directory
|
|
|
|
$cachedir = TEMP_FOLDER; // default silverstripe-cache
|
|
|
|
|
|
|
|
$cache_file = md5($url); // encoded name of cache file
|
|
|
|
$cache_path = $cachedir . "/$cache_file";
|
|
|
|
|
|
|
|
if((@file_exists("$cache_path") && ((@filemtime($cache_path) + $this->cache_expire) > (time())))){
|
|
|
|
$this->rawXML = file_get_contents($cache_path);
|
|
|
|
} else {
|
|
|
|
// not available in cache fetch from server
|
|
|
|
$ch = curl_init();
|
|
|
|
$timeout = 5;
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
|
|
|
$this->rawXML = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try using file_get_contents if cURL is not installed in your system.
|
|
|
|
// $this->rawXML = file_get_contents($url);
|
|
|
|
|
|
|
|
// results returned - from cache / live
|
|
|
|
if($this->rawXML != ""){
|
2007-12-13 01:39:33 +01:00
|
|
|
// save the response in cache
|
|
|
|
$fp = @fopen($cache_path,"w+");
|
2008-01-08 07:37:50 +01:00
|
|
|
@fwrite($fp,$this->rawXML);
|
2007-12-13 01:39:33 +01:00
|
|
|
@fclose($fp);
|
|
|
|
|
2007-11-15 23:40:36 +01:00
|
|
|
if($this->checkErrors == true) {
|
|
|
|
return $this->errorCatch($this->rawXML);
|
|
|
|
} else {
|
|
|
|
return $this->rawXML;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user_error("Invalid Response (maybe your calling to wrong URL or server unavailable)", E_USER_ERROR);
|
|
|
|
}
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets attributes as an array, of a particular type of element.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @param $xml string The source xml to parse, this could be the original response received.
|
|
|
|
* @param $collection string The name of parent node which wraps the elements, if available
|
|
|
|
* @param $element string The element we need to extract the attributes.
|
2007-08-30 02:12:23 +02:00
|
|
|
* Example : <photo id="2636" owner="123" secret="ab128" server="2">
|
|
|
|
* returns id, owner,secret and sever attribute values of all such photo elements.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function getAttributes($xml, $collection=NULL, $element=NULL){
|
|
|
|
$xml = new SimpleXMLElement($xml);
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
|
|
|
|
if($collection)
|
|
|
|
$childElements = $xml->{$collection};
|
|
|
|
if($element)
|
|
|
|
$childElements = $xml->{$collection}->{$element};
|
|
|
|
|
|
|
|
if($childElements){
|
|
|
|
foreach($childElements as $child){
|
|
|
|
$data = array();
|
|
|
|
foreach($child->attributes() as $key => $value){
|
|
|
|
$data["$key"] = Convert::raw2xml($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output->push(new ArrayData($data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an attribute of a particular element.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @param $xml string The source xml to parse, this could be the original response received.
|
|
|
|
* @param $collection string The name of the parent node which wraps the element, if available
|
|
|
|
* @param $element string The element we need to extract the attribute
|
|
|
|
* @param $attr string The name of the attribute
|
2007-08-30 02:12:23 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
function getAttribute($xml, $collection=NULL, $element=NULL, $attr){
|
|
|
|
$xml = new SimpleXMLElement($xml);
|
|
|
|
$attr_value = "";
|
|
|
|
|
|
|
|
if($collection)
|
|
|
|
$childElements = $xml->{$collection};
|
|
|
|
if($element)
|
|
|
|
$childElements = $xml->{$collection}->{$element};
|
|
|
|
|
|
|
|
if($childElements)
|
|
|
|
$attr_value = (string) $childElements[$attr];
|
|
|
|
|
|
|
|
return Convert::raw2xml($attr_value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets set of node values as an array.
|
|
|
|
* When you get to the depth in the hierachchy use node_child_subchild syntax to get the value.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @param $xml string The the source xml to parse, this could be the original response received.
|
|
|
|
* @param $collection string The name of parent node which wraps the elements, if available
|
|
|
|
* @param $element string The element we need to extract the node values.
|
2007-08-30 02:12:23 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
function getValues($xml, $collection=NULL, $element=NULL){
|
|
|
|
$xml = new SimpleXMLElement($xml);
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
|
|
|
|
$childElements = $xml;
|
|
|
|
if($collection)
|
|
|
|
$childElements = $xml->{$collection};
|
|
|
|
if($element)
|
|
|
|
$childElements = $xml->{$collection}->{$element};
|
|
|
|
|
|
|
|
if($childElements){
|
|
|
|
foreach($childElements as $child){
|
|
|
|
$data = array();
|
|
|
|
$this->getRecurseValues($child,$data);
|
|
|
|
$output->push(new ArrayData($data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRecurseValues($xml,&$data,$parent=""){
|
|
|
|
$child_count = 0;
|
|
|
|
foreach($xml as $key=>$value)
|
|
|
|
{
|
|
|
|
$child_count++;
|
|
|
|
$k = ($parent == "") ? (string)$key : $parent . "_" . (string)$key;
|
|
|
|
if($this->getRecurseValues($value,$data,$k) == 0) // no childern, aka "leaf node"
|
|
|
|
$data[$k] = Convert::raw2xml($value);
|
|
|
|
}
|
|
|
|
return $child_count;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a single node value.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @param $xml string The source xml to parse, this could be the original response received.
|
|
|
|
* @param $collection string The name of parent node which wraps the elements, if available
|
|
|
|
* @param $element string The element we need to extract the node value.
|
2007-08-30 02:12:23 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
function getValue($xml, $collection=NULL, $element=NULL){
|
|
|
|
$xml = new SimpleXMLElement($xml);
|
|
|
|
|
|
|
|
if($collection)
|
|
|
|
$childElements = $xml->{$collection};
|
|
|
|
if($element)
|
|
|
|
$childElements = $xml->{$collection}->{$element};
|
|
|
|
|
|
|
|
if($childElements)
|
|
|
|
return Convert::raw2xml($childElements);
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchValue($xml, $node=NULL){
|
|
|
|
$xml = new SimpleXMLElement($xml);
|
|
|
|
$childElements = $xml->xpath($node);
|
|
|
|
|
|
|
|
if($childElements)
|
|
|
|
return Convert::raw2xml($childElements[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchAttributes($xml, $node=NULL){
|
|
|
|
$xml = new SimpleXMLElement($xml);
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
|
|
|
|
$childElements = $xml->xpath($node);
|
|
|
|
|
|
|
|
if($childElements)
|
|
|
|
foreach($childElements as $child){
|
|
|
|
$data = array();
|
|
|
|
foreach($child->attributes() as $key => $value){
|
|
|
|
$data["$key"] = Convert::raw2xml($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output->push(new ArrayData($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Debug::show($attr_value);
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2007-11-15 23:40:36 +01:00
|
|
|
?>
|