2007-08-30 02:12:23 +02:00
|
|
|
<?php
|
|
|
|
|
2008-02-25 03:10:37 +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-02-25 03:10:37 +01:00
|
|
|
* For more info visit wiki documentation - http://doc.silverstripe.com/doku.php?id=restfulservice
|
|
|
|
* @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
|
|
|
|
2008-03-17 09:25:07 +01:00
|
|
|
protected $authUsername, $authPassword;
|
|
|
|
protected $customHeaders = array();
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Creates a new restful service.
|
|
|
|
* @param string $base Base URL of the web service eg: api.example.com
|
|
|
|
* @param int $expiry Set the cache expiry interva. Defaults to 1 hour (3600 seconds)
|
|
|
|
*/
|
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-02-25 03:10:37 +01:00
|
|
|
* @param array $params An array passed with necessary parameters.
|
2007-08-30 02:12:23 +02:00
|
|
|
*/
|
|
|
|
function setQueryString($params=NULL){
|
|
|
|
$this->queryString = http_build_query($params,'','&');
|
|
|
|
}
|
|
|
|
|
2008-03-17 09:25:07 +01:00
|
|
|
/**
|
|
|
|
* Set basic authentication
|
|
|
|
*/
|
|
|
|
function basicAuth($username, $password) {
|
|
|
|
$this->authUsername = $username;
|
|
|
|
$this->authPassword = $password;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a custom HTTP header
|
|
|
|
*/
|
|
|
|
function httpHeader($header) {
|
|
|
|
$this->customHeaders[] = $header;
|
|
|
|
}
|
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
protected function constructURL(){
|
2008-03-17 09:25:07 +01:00
|
|
|
return "$this->baseURL" . ($this->queryString ? "?$this->queryString" : "");
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects to the RESTful service and gets its response.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @todo implement authentication via cURL for
|
2007-08-30 02:12:23 +02:00
|
|
|
*/
|
|
|
|
|
2008-03-17 09:25:07 +01:00
|
|
|
function connect($subURL){
|
|
|
|
$url = $this->constructURL() . $subURL; //url for the request
|
2007-11-15 23:40:36 +01:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
//check for file exists in cache
|
|
|
|
//set the cache directory
|
|
|
|
$cachedir=TEMP_FOLDER; //default silverstrip-cache
|
2007-11-15 23:40:36 +01:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
$cache_file = md5($url); //encoded name of cache file
|
|
|
|
$cache_path = $cachedir."/$cache_file";
|
2007-11-15 23:40:36 +01:00
|
|
|
|
2008-03-17 09:25:07 +01:00
|
|
|
if( !isset($_GET['flush']) && ( @file_exists("$cache_path") && ((@filemtime($cache_path) + $this->cache_expire) > ( time() )))){
|
2007-11-15 23:40:36 +01:00
|
|
|
$this->rawXML = file_get_contents($cache_path);
|
2008-03-17 09:25:07 +01:00
|
|
|
|
|
|
|
} else {//not available in cache fetch from server
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-11-15 23:40:36 +01:00
|
|
|
$ch = curl_init();
|
|
|
|
$timeout = 5;
|
2008-02-25 03:10:37 +01:00
|
|
|
$useragent = "SilverStripe/2.2";
|
2007-11-15 23:40:36 +01:00
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
2008-02-25 03:10:37 +01:00
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
|
2007-11-15 23:40:36 +01:00
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
2008-03-17 09:25:07 +01:00
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
|
|
|
|
|
|
|
|
if($this->customHeaders) curl_setopt($ch, CURLOPT_HTTPHEADER, $this->customHeaders);
|
|
|
|
if($this->authUsername) curl_setopt($ch, CURLOPT_USERPWD, "$this->authUsername:$this->authPassword");
|
|
|
|
|
2007-11-15 23:40:36 +01:00
|
|
|
$this->rawXML = curl_exec($ch);
|
2008-03-17 09:25:07 +01:00
|
|
|
if($this->rawXML === false) {
|
2008-03-17 10:11:24 +01:00
|
|
|
$curlError = curl_error($ch);
|
|
|
|
// Problem verifying the server SSL certificate; just ignore it as it's not mandatory
|
|
|
|
if(strpos($curlError,'14090086') !== false) {
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
$this->rawXML = curl_exec($ch);
|
|
|
|
$curlError = curl_error($ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->rawXML === false) {
|
|
|
|
user_error("Curl Error:" . $curlError, E_USER_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
2008-03-17 09:25:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
2007-11-15 23:40:36 +01:00
|
|
|
curl_close($ch);
|
|
|
|
|
2008-03-17 09:25:07 +01:00
|
|
|
// Success
|
|
|
|
if($statusCode >= 200 && $statusCode < 300) return $this->rawXML;
|
|
|
|
|
|
|
|
// Failure
|
|
|
|
switch($statusCode) {
|
|
|
|
case 401:
|
|
|
|
user_error("Bad username/password given to RestfulService, url $url", E_USER_WARNING);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
user_error("Error code $statusCode from url $url", E_USER_WARNING);
|
|
|
|
break;
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-11-15 23:40:36 +01:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
//Try using file_get_contents if cURL is not installed in your system.
|
|
|
|
//$this->rawXML = file_get_contents($url);
|
2007-11-15 23:40:36 +01:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
//results returned - from cache / live
|
2007-11-15 23:40:36 +01:00
|
|
|
if($this->rawXML != ""){
|
2008-02-25 03:10:37 +01:00
|
|
|
//save the response in cache
|
|
|
|
$fp = @fopen($cache_path,"w+");
|
|
|
|
@fwrite($fp,$this->rawXML);
|
|
|
|
@fclose($fp);
|
|
|
|
|
2007-11-15 23:40:36 +01:00
|
|
|
if($this->checkErrors == true) {
|
|
|
|
return $this->errorCatch($this->rawXML);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
|
|
|
else {
|
2007-11-15 23:40:36 +01:00
|
|
|
return $this->rawXML;
|
|
|
|
}
|
2008-03-17 09:25:07 +01:00
|
|
|
} else {
|
2007-11-15 23:40:36 +01:00
|
|
|
user_error("Invalid Response (maybe your calling to wrong URL or server unavailable)", E_USER_ERROR);
|
|
|
|
}
|
2008-03-17 09:25:07 +01:00
|
|
|
}
|
2007-08-30 02:12:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets attributes as an array, of a particular type of element.
|
|
|
|
* Example : <photo id="2636" owner="123" secret="ab128" server="2">
|
|
|
|
* returns id, owner,secret and sever attribute values of all such photo elements.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @param string $xml The source xml to parse, this could be the original response received.
|
|
|
|
* @param string $collection The name of parent node which wraps the elements, if available
|
|
|
|
* @param string $element The element we need to extract the attributes.
|
2007-08-30 02:12:23 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
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-02-25 03:10:37 +01:00
|
|
|
* @param string $xml The source xml to parse, this could be the original response received.
|
|
|
|
* @param string $collection The name of the parent node which wraps the element, if available
|
|
|
|
* @param string $element The element we need to extract the attribute
|
|
|
|
* @param string $attr 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-02-25 03:10:37 +01:00
|
|
|
* @param string $xml The the source xml to parse, this could be the original response received.
|
|
|
|
* @param string $collection The name of parent node which wraps the elements, if available
|
|
|
|
* @param string $element 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=""){
|
2008-02-25 03:10:37 +01:00
|
|
|
$conv_value = "";
|
2007-08-30 02:12:23 +02:00
|
|
|
$child_count = 0;
|
|
|
|
foreach($xml as $key=>$value)
|
|
|
|
{
|
|
|
|
$child_count++;
|
|
|
|
$k = ($parent == "") ? (string)$key : $parent . "_" . (string)$key;
|
2008-02-25 03:10:37 +01:00
|
|
|
if($this->getRecurseValues($value,$data,$k) == 0){ // no childern, aka "leaf node"
|
|
|
|
$conv_value = Convert::raw2xml($value);
|
|
|
|
}
|
|
|
|
//Review the fix for similar node names overriding it's predecessor
|
|
|
|
if(array_key_exists($k, $data) == true) {
|
|
|
|
$data[$k] = $data[$k] . ",". $conv_value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$data[$k] = $conv_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
|
|
|
return $child_count;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a single node value.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @param string $xml The source xml to parse, this could be the original response received.
|
|
|
|
* @param string $collection The name of parent node which wraps the elements, if available
|
|
|
|
* @param string $element 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);
|
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Searches for a node in document tree and returns it value.
|
|
|
|
* @param string $xml source xml to parse, this could be the original response received.
|
|
|
|
* @param string $node Node to search for
|
|
|
|
*/
|
2007-08-30 02:12:23 +02:00
|
|
|
function searchValue($xml, $node=NULL){
|
|
|
|
$xml = new SimpleXMLElement($xml);
|
|
|
|
$childElements = $xml->xpath($node);
|
|
|
|
|
|
|
|
if($childElements)
|
|
|
|
return Convert::raw2xml($childElements[0]);
|
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Searches for a node in document tree and returns its attributes.
|
|
|
|
* @param string $xml the source xml to parse, this could be the original response received.
|
|
|
|
* @param string $node Node to search for
|
|
|
|
*/
|
2007-08-30 02:12:23 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2008-03-17 09:25:07 +01:00
|
|
|
?>
|