2007-08-30 02:12:23 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RestfulService class allows you to consume various RESTful APIs.
|
|
|
|
* Through this you could connect and aggregate data of various web services.
|
2014-08-15 08:53:05 +02:00
|
|
|
* For more info visit wiki documentation - http://doc.silverstripe.org/doku.php?id=restfulservice
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage integration
|
2007-08-30 02:12:23 +02:00
|
|
|
*/
|
2014-08-16 02:51:17 +02:00
|
|
|
class RestfulService extends ViewableData implements Flushable {
|
2013-12-01 03:12:54 +01:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
protected $baseURL;
|
|
|
|
protected $queryString;
|
|
|
|
protected $errorTag;
|
2007-11-15 23:40:36 +01:00
|
|
|
protected $checkErrors;
|
|
|
|
protected $cache_expire;
|
2008-03-17 09:25:07 +01:00
|
|
|
protected $authUsername, $authPassword;
|
|
|
|
protected $customHeaders = array();
|
2010-04-12 05:39:14 +02:00
|
|
|
protected $proxy;
|
2013-03-21 19:48:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $default_proxy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $default_curl_options = array();
|
2013-01-31 17:41:24 +01:00
|
|
|
|
2014-08-16 02:51:17 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var bool Flushes caches if set to true. This is set by {@link flush()}
|
|
|
|
*/
|
|
|
|
private static $flush = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggered early in the request when someone requests a flush.
|
|
|
|
*/
|
|
|
|
public static function flush() {
|
|
|
|
self::$flush = true;
|
|
|
|
}
|
|
|
|
|
2013-01-31 17:41:24 +01:00
|
|
|
/**
|
|
|
|
* set a curl option that will be applied to all requests as default
|
|
|
|
* {@see http://php.net/manual/en/function.curl-setopt.php#refsect1-function.curl-setopt-parameters}
|
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @deprecated 3.2 Use the "RestfulService.default_curl_options" config setting instead
|
2013-01-31 17:41:24 +01:00
|
|
|
* @param int $option The cURL opt Constant
|
|
|
|
* @param mixed $value The cURL opt value
|
|
|
|
*/
|
|
|
|
public static function set_default_curl_option($option, $value) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "RestfulService.default_curl_options" config setting instead');
|
|
|
|
Config::inst()->update('RestfulService', 'default_curl_options', array($option => $value));
|
2013-01-31 17:41:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set many defauly curl options at once
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @deprecated 3.2 Use the "RestfulService.default_curl_options" config setting instead
|
2013-01-31 17:41:24 +01:00
|
|
|
*/
|
|
|
|
public static function set_default_curl_options($optionArray) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "RestfulService.default_curl_options" config setting instead');
|
|
|
|
Config::inst()->update('RestfulService', 'default_curl_options', $optionArray);
|
2013-01-31 17:41:24 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:39:14 +02:00
|
|
|
/**
|
|
|
|
* Sets default proxy settings for outbound RestfulService connections
|
|
|
|
*
|
|
|
|
* @param string $proxy The URL of the proxy to use.
|
|
|
|
* @param int $port Proxy port
|
|
|
|
* @param string $user The proxy auth user name
|
|
|
|
* @param string $password The proxy auth password
|
|
|
|
* @param boolean $socks Set true to use socks5 proxy instead of http
|
2013-03-21 19:48:54 +01:00
|
|
|
* @deprecated 3.2 Use the "RestfulService.default_curl_options" config setting instead,
|
|
|
|
* with direct reference to the CURL_* options
|
2010-04-12 05:39:14 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_default_proxy($proxy, $port = 80, $user = "", $password = "", $socks = false) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Deprecation::notice(
|
2014-08-15 08:53:05 +02:00
|
|
|
'3.1',
|
|
|
|
'Use the "RestfulService.default_curl_options" config setting instead, '
|
2013-03-21 19:48:54 +01:00
|
|
|
. 'with direct reference to the CURL_* options'
|
|
|
|
);
|
2013-03-26 00:02:46 +01:00
|
|
|
config::inst()->update('RestfulService', 'default_proxy', array(
|
2010-04-12 05:39:14 +02:00
|
|
|
CURLOPT_PROXY => $proxy,
|
|
|
|
CURLOPT_PROXYUSERPWD => "{$user}:{$password}",
|
|
|
|
CURLOPT_PROXYPORT => $port,
|
|
|
|
CURLOPT_PROXYTYPE => ($socks ? CURLPROXY_SOCKS5 : CURLPROXY_HTTP)
|
2013-03-21 19:48:54 +01:00
|
|
|
));
|
2010-04-12 05:39:14 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Creates a new restful service.
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $base Base URL of the web service eg: api.example.com
|
2008-02-25 03:10:37 +01:00
|
|
|
* @param int $expiry Set the cache expiry interva. Defaults to 1 hour (3600 seconds)
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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;
|
2009-09-18 05:02:19 +02:00
|
|
|
parent::__construct();
|
2013-03-21 19:48:54 +01:00
|
|
|
$this->proxy = $this->config()->default_proxy;
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
/**
|
|
|
|
* Sets the Query string parameters to send a request.
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param array $params An array passed with necessary parameters.
|
2007-08-30 02:12:23 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setQueryString($params=NULL){
|
2007-08-30 02:12:23 +02:00
|
|
|
$this->queryString = http_build_query($params,'','&');
|
|
|
|
}
|
2010-04-12 05:39:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set proxy settings for this RestfulService instance
|
|
|
|
*
|
|
|
|
* @param string $proxy The URL of the proxy to use.
|
|
|
|
* @param int $port Proxy port
|
|
|
|
* @param string $user The proxy auth user name
|
|
|
|
* @param string $password The proxy auth password
|
|
|
|
* @param boolean $socks Set true to use socks5 proxy instead of http
|
|
|
|
*/
|
2012-12-08 12:20:20 +01:00
|
|
|
public function setProxy($proxy, $port = 80, $user = "", $password = "", $socks = false) {
|
2010-04-12 05:39:14 +02:00
|
|
|
$this->proxy = array(
|
|
|
|
CURLOPT_PROXY => $proxy,
|
|
|
|
CURLOPT_PROXYUSERPWD => "{$user}:{$password}",
|
|
|
|
CURLOPT_PROXYPORT => $port,
|
2014-08-15 08:53:05 +02:00
|
|
|
CURLOPT_PROXYTYPE => ($socks ? CURLPROXY_SOCKS5 : CURLPROXY_HTTP)
|
2010-04-12 05:39:14 +02:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-17 09:25:07 +01:00
|
|
|
/**
|
|
|
|
* Set basic authentication
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function basicAuth($username, $password) {
|
2008-03-17 09:25:07 +01:00
|
|
|
$this->authUsername = $username;
|
|
|
|
$this->authPassword = $password;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-17 09:25:07 +01:00
|
|
|
/**
|
|
|
|
* Set a custom HTTP header
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function httpHeader($header) {
|
2008-03-17 09:25:07 +01:00
|
|
|
$this->customHeaders[] = $header;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
protected function constructURL(){
|
2014-05-07 20:43:54 +02:00
|
|
|
Deprecation::notice('3.2', 'constructURL is deprecated, please use `getAbsoluteRequestURL` instead');
|
|
|
|
return Controller::join_links($this->baseURL, '?' . $this->queryString);
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-31 01:17:36 +02:00
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Makes a request to the RESTful server, and return a {@link RestfulService_Response} object for parsing of the
|
|
|
|
* result.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-03-31 01:17:36 +02:00
|
|
|
* @todo Better POST, PUT, DELETE, and HEAD support
|
|
|
|
* @todo Caching of requests - probably only GET and HEAD requestst
|
|
|
|
* @todo JSON support in RestfulService_Response
|
|
|
|
* @todo Pass the response headers to RestfulService_Response
|
|
|
|
*
|
|
|
|
* This is a replacement of {@link connect()}.
|
2010-10-19 02:36:48 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* @return RestfulService_Response - If curl request produces error, the returned response's status code will
|
|
|
|
* be 500
|
2008-03-31 01:17:36 +02:00
|
|
|
*/
|
2010-04-14 04:20:57 +02:00
|
|
|
public function request($subURL = '', $method = "GET", $data = null, $headers = null, $curlOptions = array()) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
$url = $this->getAbsoluteRequestURL($subURL);
|
2008-03-31 01:17:36 +02:00
|
|
|
$method = strtoupper($method);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-31 01:17:36 +02:00
|
|
|
assert(in_array($method, array('GET','POST','PUT','DELETE','HEAD','OPTIONS')));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-30 18:10:37 +01:00
|
|
|
$cache_path = $this->getCachePath(array(
|
|
|
|
$url,
|
2013-01-30 17:39:18 +01:00
|
|
|
$method,
|
2013-01-30 18:10:37 +01:00
|
|
|
$data,
|
|
|
|
array_merge((array)$this->customHeaders, (array)$headers),
|
2013-03-21 19:48:54 +01:00
|
|
|
$curlOptions + (array)$this->config()->default_curl_options,
|
2013-01-30 18:10:37 +01:00
|
|
|
$this->getBasicAuthString()
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
// Check for unexpired cached feed (unless flush is set)
|
2013-01-30 18:10:37 +01:00
|
|
|
//assume any cache_expire that is 0 or less means that we dont want to
|
|
|
|
// cache
|
2014-08-16 02:51:17 +02:00
|
|
|
if($this->cache_expire > 0 && self::$flush
|
2013-01-30 18:10:37 +01:00
|
|
|
&& @file_exists($cache_path)
|
2012-09-26 23:34:00 +02:00
|
|
|
&& @filemtime($cache_path) + $this->cache_expire > time()) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
$store = file_get_contents($cache_path);
|
|
|
|
$response = unserialize($store);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
} else {
|
2011-03-23 10:05:29 +01:00
|
|
|
$response = $this->curlRequest($url, $method, $data, $headers, $curlOptions);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:05:29 +01:00
|
|
|
if(!$response->isError()) {
|
2010-10-19 02:36:48 +02:00
|
|
|
// Serialise response object and write to cache
|
|
|
|
$store = serialize($response);
|
|
|
|
file_put_contents($cache_path, $store);
|
|
|
|
}
|
|
|
|
else {
|
2014-08-15 08:53:05 +02:00
|
|
|
// In case of curl or/and http indicate error, populate response's cachedBody property
|
|
|
|
// with cached response body with the cache file exists
|
2010-10-19 02:36:48 +02:00
|
|
|
if (@file_exists($cache_path)) {
|
|
|
|
$store = file_get_contents($cache_path);
|
|
|
|
$cachedResponse = unserialize($store);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-30 18:10:37 +01:00
|
|
|
$response->setCachedResponse($cachedResponse);
|
2010-10-19 02:36:48 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-01-30 18:10:37 +01:00
|
|
|
$response->setCachedResponse(false);
|
2010-10-19 02:36:48 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-05 07:19:48 +01:00
|
|
|
}
|
|
|
|
|
2008-03-31 01:17:36 +02:00
|
|
|
return $response;
|
|
|
|
}
|
2011-03-23 10:05:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Actually performs a remote service request using curl. This is used by
|
|
|
|
* {@link RestfulService::request()}.
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param string $method
|
|
|
|
* @param array $data
|
|
|
|
* @param array $headers
|
|
|
|
* @param array $curlOptions
|
|
|
|
* @return RestfulService_Response
|
|
|
|
*/
|
|
|
|
public function curlRequest($url, $method, $data = null, $headers = null, $curlOptions = array()) {
|
|
|
|
$ch = curl_init();
|
2014-05-06 23:13:56 +02:00
|
|
|
$timeout = 5;
|
2014-08-15 08:53:05 +02:00
|
|
|
$sapphireInfo = new SapphireInfo();
|
2012-04-30 10:22:00 +02:00
|
|
|
$useragent = 'SilverStripe/' . $sapphireInfo->Version();
|
2013-03-21 19:48:54 +01:00
|
|
|
$curlOptions = $curlOptions + (array)$this->config()->default_curl_options;
|
2011-03-23 10:05:29 +01:00
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
|
2014-05-06 23:13:56 +02:00
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
2011-03-23 10:05:29 +01:00
|
|
|
if(!ini_get('open_basedir')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
2013-09-20 06:26:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Write headers to a temporary file
|
|
|
|
$headerfd = tmpfile();
|
|
|
|
curl_setopt($ch, CURLOPT_WRITEHEADER, $headerfd);
|
2011-03-23 10:05:29 +01:00
|
|
|
|
|
|
|
// Add headers
|
|
|
|
if($this->customHeaders) {
|
|
|
|
$headers = array_merge((array)$this->customHeaders, (array)$headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
|
|
|
|
// Add authentication
|
2013-01-30 18:10:37 +01:00
|
|
|
if($this->authUsername) curl_setopt($ch, CURLOPT_USERPWD, $this->getBasicAuthString());
|
2011-03-23 10:05:29 +01:00
|
|
|
|
|
|
|
// Add fields to POST and PUT requests
|
|
|
|
if($method == 'POST') {
|
|
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
|
} elseif($method == 'PUT') {
|
2014-08-15 08:53:05 +02:00
|
|
|
$put = fopen("php://temp", 'r+');
|
2011-03-23 10:05:29 +01:00
|
|
|
fwrite($put, $data);
|
2014-08-15 08:53:05 +02:00
|
|
|
fseek($put, 0);
|
2011-03-23 10:05:29 +01:00
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_PUT, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_INFILE, $put);
|
2014-08-15 08:53:05 +02:00
|
|
|
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
|
2011-03-23 10:05:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply proxy settings
|
|
|
|
if(is_array($this->proxy)) {
|
|
|
|
curl_setopt_array($ch, $this->proxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set any custom options passed to the request() function
|
|
|
|
curl_setopt_array($ch, $curlOptions);
|
|
|
|
|
|
|
|
// Run request
|
2013-09-20 06:26:39 +02:00
|
|
|
$body = curl_exec($ch);
|
|
|
|
|
|
|
|
rewind($headerfd);
|
|
|
|
$headers = stream_get_contents($headerfd);
|
|
|
|
fclose($headerfd);
|
|
|
|
|
|
|
|
$response = $this->extractResponse($ch, $headers, $body);
|
2011-03-23 10:05:29 +01:00
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2013-01-30 18:10:37 +01:00
|
|
|
/**
|
|
|
|
* A function to return the auth string. This helps consistency through the
|
|
|
|
* class but also allows tests to pull it out when generating the expected
|
|
|
|
* cache keys
|
|
|
|
*
|
|
|
|
* @see {self::getCachePath()}
|
|
|
|
* @see {RestfulServiceTest::createFakeCachedResponse()}
|
|
|
|
*
|
|
|
|
* @return string The auth string to be base64 encoded
|
|
|
|
*/
|
|
|
|
protected function getBasicAuthString() {
|
|
|
|
return $this->authUsername . ':' . $this->authPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a cache key based on any cache data sent. The cache data can be
|
|
|
|
* any type
|
|
|
|
*
|
|
|
|
* @param mixed $cacheData The cache seed for generating the key
|
|
|
|
* @param string the md5 encoded cache seed.
|
|
|
|
*/
|
|
|
|
protected function generateCacheKey($cacheData) {
|
|
|
|
return md5(var_export($cacheData, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the cache path
|
|
|
|
*
|
|
|
|
* This is mainly so that the cache path can be generated in a consistent
|
|
|
|
* way in tests without having to hard code the cachekey generate function
|
|
|
|
* in tests
|
|
|
|
*
|
|
|
|
* @param mixed $cacheData The cache seed {@see self::generateCacheKey}
|
|
|
|
*
|
|
|
|
* @return string The path to the cache file
|
|
|
|
*/
|
|
|
|
protected function getCachePath($cacheData) {
|
|
|
|
return TEMP_FOLDER . "/xmlresponse_" . $this->generateCacheKey($cacheData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-20 06:26:39 +02:00
|
|
|
* Extracts the response body and headers from a full curl response
|
2013-01-30 18:10:37 +01:00
|
|
|
*
|
|
|
|
* @param curl_handle $ch The curl handle for the request
|
|
|
|
* @param string $rawResponse The raw response text
|
2013-01-31 17:41:24 +01:00
|
|
|
*
|
|
|
|
* @return RestfulService_Response The response object
|
2013-01-30 18:10:37 +01:00
|
|
|
*/
|
2013-09-20 06:26:39 +02:00
|
|
|
protected function extractResponse($ch, $rawHeaders, $rawBody) {
|
2013-01-31 17:41:24 +01:00
|
|
|
//get the status code
|
|
|
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
2013-02-19 13:07:22 +01:00
|
|
|
//get a curl error if there is one
|
|
|
|
$curlError = curl_error($ch);
|
2013-01-31 17:41:24 +01:00
|
|
|
//normalise the status code
|
2013-02-23 02:41:55 +01:00
|
|
|
if(curl_error($ch) !== '' || $statusCode == 0) $statusCode = 500;
|
2013-09-20 06:26:39 +02:00
|
|
|
//parse the headers
|
|
|
|
$parts = array_filter(explode("\r\n\r\n", $rawHeaders));
|
|
|
|
$lastHeaders = array_pop($parts);
|
|
|
|
$headers = $this->parseRawHeaders($lastHeaders);
|
|
|
|
//return the response object
|
|
|
|
return new RestfulService_Response($rawBody, $statusCode, $headers);
|
2013-01-30 18:10:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes raw headers and parses them to turn them to an associative array
|
|
|
|
*
|
|
|
|
* Any header that we see more than once is turned into an array.
|
|
|
|
*
|
2013-01-31 17:41:24 +01:00
|
|
|
* This is meant to mimic http_parse_headers {@link http://php.net/manual/en/function.http-parse-headers.php}
|
2013-01-30 18:10:37 +01:00
|
|
|
* thanks to comment #77241 on that page for foundation of this
|
|
|
|
*
|
|
|
|
* @param string $rawHeaders The raw header string
|
|
|
|
* @return array The assosiative array of headers
|
|
|
|
*/
|
|
|
|
protected function parseRawHeaders($rawHeaders) {
|
|
|
|
$headers = array();
|
|
|
|
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $rawHeaders));
|
|
|
|
foreach( $fields as $field ) {
|
|
|
|
if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
|
|
|
|
$match[1] = preg_replace_callback(
|
|
|
|
'/(?<=^|[\x09\x20\x2D])./',
|
|
|
|
create_function('$matches', 'return strtoupper($matches[0]);'),
|
2013-07-10 14:00:40 +02:00
|
|
|
trim($match[1])
|
2013-01-30 18:10:37 +01:00
|
|
|
);
|
|
|
|
if( isset($headers[$match[1]]) ) {
|
|
|
|
if (!is_array($headers[$match[1]])) {
|
|
|
|
$headers[$match[1]] = array($headers[$match[1]]);
|
|
|
|
}
|
|
|
|
$headers[$match[1]][] = $match[2];
|
2013-01-31 17:41:24 +01:00
|
|
|
} else {
|
|
|
|
$headers[$match[1]] = trim($match[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $headers;
|
2013-01-30 18:10:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
2010-10-19 02:36:48 +02:00
|
|
|
* Returns a full request url
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string
|
|
|
|
*/
|
2014-05-07 20:43:54 +02:00
|
|
|
public function getAbsoluteRequestURL($subURL = '') {
|
|
|
|
$url = Controller::join_links($this->baseURL, $subURL, '?' . $this->queryString);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:36:48 +02:00
|
|
|
return str_replace(' ', '%20', $url); // Encode spaces
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
/**
|
|
|
|
* Gets attributes as an array, of a particular type of element.
|
2014-08-15 08:53:05 +02:00
|
|
|
* Example : <photo id="2636" owner="123" secret="ab128" server="2">
|
2007-08-30 02:12:23 +02:00
|
|
|
* 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
|
|
|
*/
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 04:57:59 +02:00
|
|
|
public function getAttributes($xml, $collection=NULL, $element=NULL){
|
2007-08-30 02:12:23 +02:00
|
|
|
$xml = new SimpleXMLElement($xml);
|
2011-05-05 12:40:24 +02:00
|
|
|
$output = new ArrayList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
if($collection)
|
|
|
|
$childElements = $xml->{$collection};
|
|
|
|
if($element)
|
|
|
|
$childElements = $xml->{$collection}->{$element};
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
if($childElements){
|
2009-01-05 07:19:48 +01:00
|
|
|
foreach($childElements as $child){
|
|
|
|
$data = array();
|
|
|
|
foreach($child->attributes() as $key => $value){
|
|
|
|
$data["$key"] = Convert::raw2xml($value);
|
|
|
|
}
|
|
|
|
$output->push(new ArrayData($data));
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 04:57:59 +02:00
|
|
|
public function getAttribute($xml, $collection=NULL, $element=NULL, $attr){
|
|
|
|
$xml = new SimpleXMLElement($xml);
|
|
|
|
$attr_value = "";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 04:57:59 +02:00
|
|
|
if($collection)
|
2007-08-30 02:12:23 +02:00
|
|
|
$childElements = $xml->{$collection};
|
|
|
|
if($element)
|
|
|
|
$childElements = $xml->{$collection}->{$element};
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
if($childElements)
|
|
|
|
$attr_value = (string) $childElements[$attr];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
return Convert::raw2xml($attr_value);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Gets set of node values as an array.
|
2011-10-19 00:32:52 +02:00
|
|
|
* When you get to the depth in the hierarchy 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
|
|
|
*/
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 04:57:59 +02:00
|
|
|
public function getValues($xml, $collection=NULL, $element=NULL){
|
2007-08-30 02:12:23 +02:00
|
|
|
$xml = new SimpleXMLElement($xml);
|
2011-05-05 12:40:24 +02:00
|
|
|
$output = new ArrayList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
$childElements = $xml;
|
|
|
|
if($collection)
|
|
|
|
$childElements = $xml->{$collection};
|
|
|
|
if($element)
|
|
|
|
$childElements = $xml->{$collection}->{$element};
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
if($childElements){
|
2008-08-09 05:29:30 +02:00
|
|
|
foreach($childElements as $child){
|
|
|
|
$data = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->getRecurseValues($child,$data);
|
2008-08-09 05:29:30 +02:00
|
|
|
$output->push(new ArrayData($data));
|
|
|
|
}
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
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)
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
$child_count++;
|
2007-08-30 02:12:23 +02:00
|
|
|
$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"
|
2012-12-08 12:20:20 +01:00
|
|
|
$conv_value = Convert::raw2xml($value);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
//Review the fix for similar node names overriding it's predecessor
|
2014-08-15 08:53:05 +02:00
|
|
|
if(array_key_exists($k, $data) == true) {
|
|
|
|
$data[$k] = $data[$k] . ",". $conv_value;
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-12-08 12:20:20 +01:00
|
|
|
$data[$k] = $conv_value;
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
|
|
|
return $child_count;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* 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
|
|
|
*/
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getValue($xml, $collection=NULL, $element=NULL){
|
2007-08-30 02:12:23 +02:00
|
|
|
$xml = new SimpleXMLElement($xml);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
if($collection)
|
|
|
|
$childElements = $xml->{$collection};
|
|
|
|
if($element)
|
|
|
|
$childElements = $xml->{$collection}->{$element};
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
if($childElements)
|
|
|
|
return Convert::raw2xml($childElements);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Searches for a node in document tree and returns it value.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @param string $xml source xml to parse, this could be the original response received.
|
|
|
|
* @param string $node Node to search for
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function searchValue($xml, $node=NULL){
|
2007-08-30 02:12:23 +02:00
|
|
|
$xml = new SimpleXMLElement($xml);
|
|
|
|
$childElements = $xml->xpath($node);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
if($childElements)
|
|
|
|
return Convert::raw2xml($childElements[0]);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Searches for a node in document tree and returns its attributes.
|
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 $node Node to search for
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function searchAttributes($xml, $node=NULL){
|
2007-08-30 02:12:23 +02:00
|
|
|
$xml = new SimpleXMLElement($xml);
|
2011-05-05 12:40:24 +02:00
|
|
|
$output = new ArrayList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
$childElements = $xml->xpath($node);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
if($childElements)
|
|
|
|
foreach($childElements as $child){
|
|
|
|
$data = array();
|
|
|
|
foreach($child->attributes() as $key => $value){
|
|
|
|
$data["$key"] = Convert::raw2xml($value);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
$output->push(new ArrayData($data));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-30 02:12:23 +02:00
|
|
|
return $output;
|
|
|
|
}
|
2008-03-31 01:17:36 +02:00
|
|
|
}
|
|
|
|
|
2010-04-23 02:11:41 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-04-23 02:11:41 +02:00
|
|
|
* @subpackage integration
|
|
|
|
*/
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
class RestfulService_Response extends SS_HTTPResponse {
|
2008-03-31 01:17:36 +02:00
|
|
|
protected $simpleXML;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:36:48 +02:00
|
|
|
/**
|
2013-01-31 17:41:24 +01:00
|
|
|
* @var boolean It should be populated with cached request
|
2010-10-19 02:36:48 +02:00
|
|
|
* when a request referring to this response was unsuccessful
|
|
|
|
*/
|
2013-01-30 18:10:37 +01:00
|
|
|
protected $cachedResponse = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($body, $statusCode = 200, $headers = null) {
|
2008-03-31 01:17:36 +02:00
|
|
|
$this->setbody($body);
|
|
|
|
$this->setStatusCode($statusCode);
|
|
|
|
$this->headers = $headers;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function simpleXML() {
|
2009-07-02 09:18:18 +02:00
|
|
|
if(!$this->simpleXML) {
|
|
|
|
try {
|
|
|
|
$this->simpleXML = new SimpleXMLElement($this->body);
|
|
|
|
}
|
|
|
|
catch(Exception $e) {
|
|
|
|
user_error("String could not be parsed as XML. " . $e, E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
2008-03-31 01:17:36 +02:00
|
|
|
return $this->simpleXML;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-01-30 18:10:37 +01:00
|
|
|
/**
|
|
|
|
* get the cached response object. This allows you to access the cached
|
|
|
|
* eaders, not just the cached body.
|
|
|
|
*
|
|
|
|
* @return RestfulSerivice_Response The cached response object
|
|
|
|
*/
|
|
|
|
public function getCachedResponse() {
|
|
|
|
return $this->cachedResponse;
|
|
|
|
}
|
|
|
|
|
2010-10-19 02:36:48 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCachedBody() {
|
2013-01-30 18:10:37 +01:00
|
|
|
if ($this->cachedResponse) {
|
|
|
|
return $this->cachedResponse->getBody();
|
|
|
|
}
|
|
|
|
return false;
|
2010-10-19 02:36:48 +02:00
|
|
|
}
|
2013-01-31 17:41:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
public function setCachedBody($content) {
|
2013-03-20 10:00:51 +01:00
|
|
|
Deprecation::notice('3.2', 'Setting the response body is now deprecated, set the cached request instead');
|
2013-01-31 17:41:24 +01:00
|
|
|
if (!$this->cachedResponse) {
|
|
|
|
$this->cachedResponse = new RestfulService_Response($content);
|
|
|
|
}
|
|
|
|
else {
|
2013-02-18 12:55:11 +01:00
|
|
|
$this->cachedResponse->setBody($content);
|
2013-01-31 17:41:24 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:36:48 +02:00
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*/
|
2013-01-30 18:10:37 +01:00
|
|
|
public function setCachedResponse($response) {
|
|
|
|
$this->cachedResponse = $response;
|
2010-10-19 02:36:48 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-31 01:17:36 +02:00
|
|
|
/**
|
|
|
|
* Return an array of xpath matches
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function xpath($xpath) {
|
2008-03-31 01:17:36 +02:00
|
|
|
return $this->simpleXML()->xpath($xpath);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-31 01:17:36 +02:00
|
|
|
/**
|
|
|
|
* Return the first xpath match
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function xpath_one($xpath) {
|
2008-03-31 01:17:36 +02:00
|
|
|
$items = $this->xpath($xpath);
|
2013-01-30 18:10:37 +01:00
|
|
|
if (isset($items[0])) {
|
|
|
|
return $items[0];
|
|
|
|
}
|
2008-03-31 01:17:36 +02:00
|
|
|
}
|
2007-08-30 02:12:23 +02:00
|
|
|
}
|