mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX: [dio5] #2549 - Fixed bugs in RestfulService::connect()
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@58796 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
7b278290b4
commit
b3a098fe32
@ -58,24 +58,27 @@ class RestfulService extends ViewableData {
|
|||||||
* Connects to the RESTful service and gets its response.
|
* Connects to the RESTful service and gets its response.
|
||||||
* @deprecated Use {@link request()} instead.
|
* @deprecated Use {@link request()} instead.
|
||||||
*/
|
*/
|
||||||
function connect($subURL){
|
function connect($subURL = ""){
|
||||||
$url = $this->constructURL() . $subURL; //url for the request
|
|
||||||
|
|
||||||
//check for file exists in cache
|
// url for the request
|
||||||
//set the cache directory
|
$url = $this->constructURL() . $subURL;
|
||||||
$cachedir=TEMP_FOLDER; //default silverstrip-cache
|
|
||||||
|
|
||||||
|
// set the cache directory
|
||||||
|
$cachedir=TEMP_FOLDER; // default silverstrip-cache
|
||||||
$cache_file = md5($url); //encoded name of cache file
|
$cache_file = md5($url); //encoded name of cache file
|
||||||
$cache_path = $cachedir."/$cache_file";
|
$cache_path = $cachedir."/$cache_file";
|
||||||
|
|
||||||
if( !isset($_GET['flush']) && ( @file_exists("$cache_path") && ((@filemtime($cache_path) + $this->cache_expire) > ( time() )))){
|
// check for file exists in cache
|
||||||
|
if( !isset($_GET['flush']) && ( @file_exists("$cache_path") && ((@filemtime($cache_path) + $this->cache_expire) > ( time() )))) {
|
||||||
$this->rawXML = file_get_contents($cache_path);
|
$this->rawXML = file_get_contents($cache_path);
|
||||||
|
return $this->rawXML;
|
||||||
|
} else {
|
||||||
|
// not available in cache fetch from server
|
||||||
|
|
||||||
} else {//not available in cache fetch from server
|
$statusOK = false;
|
||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
$timeout = 5;
|
$timeout = 5;
|
||||||
$useragent = "SilverStripe/2.2";
|
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0';
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
|
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
|
||||||
@ -86,45 +89,37 @@ class RestfulService extends ViewableData {
|
|||||||
if($this->authUsername) curl_setopt($ch, CURLOPT_USERPWD, "$this->authUsername:$this->authPassword");
|
if($this->authUsername) curl_setopt($ch, CURLOPT_USERPWD, "$this->authUsername:$this->authPassword");
|
||||||
|
|
||||||
$this->rawXML = curl_exec($ch);
|
$this->rawXML = curl_exec($ch);
|
||||||
if($this->rawXML === false) {
|
|
||||||
$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) {
|
if($this->rawXML === false) {
|
||||||
user_error("Curl Error:" . $curlError, E_USER_WARNING);
|
user_error("Curl Error:" . curl_error($ch), E_USER_WARNING);
|
||||||
return;
|
return NULL;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
// Success
|
// Success
|
||||||
if($statusCode >= 200 && $statusCode < 300) return $this->rawXML;
|
if($statusCode >= 200 && $statusCode < 300) {
|
||||||
|
// fill output with xml and continue
|
||||||
// Failure
|
$statusOK = true;
|
||||||
|
} else {
|
||||||
|
// Failure: throw error and return NULL
|
||||||
switch($statusCode) {
|
switch($statusCode) {
|
||||||
case 401:
|
case 401:
|
||||||
user_error("Bad username/password given to RestfulService, url $url", E_USER_WARNING);
|
user_error("Bad username/password given to RestfulService, url $url", E_USER_WARNING);
|
||||||
break;
|
return NULL;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
user_error("Error code $statusCode from url $url", E_USER_WARNING);
|
user_error("Error code $statusCode from url $url", E_USER_WARNING);
|
||||||
break;
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
// Try using file_get_contents if cURL is not installed in your system.
|
||||||
|
// $this->rawXML = file_get_contents($url);
|
||||||
|
|
||||||
//Try using file_get_contents if cURL is not installed in your system.
|
// results returned - from cache / live
|
||||||
//$this->rawXML = file_get_contents($url);
|
if($statusOK && $this->rawXML != "") {
|
||||||
|
|
||||||
//results returned - from cache / live
|
|
||||||
if($this->rawXML != ""){
|
|
||||||
//save the response in cache
|
//save the response in cache
|
||||||
$fp = @fopen($cache_path,"w+");
|
$fp = @fopen($cache_path,"w+");
|
||||||
@fwrite($fp,$this->rawXML);
|
@fwrite($fp,$this->rawXML);
|
||||||
@ -132,12 +127,15 @@ class RestfulService extends ViewableData {
|
|||||||
|
|
||||||
if($this->checkErrors == true) {
|
if($this->checkErrors == true) {
|
||||||
return $this->errorCatch($this->rawXML);
|
return $this->errorCatch($this->rawXML);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return $this->rawXML;
|
return $this->rawXML;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// I think this following else can actually be removed :P
|
||||||
} else {
|
} else {
|
||||||
user_error("Invalid Response (maybe your calling to wrong URL or server unavailable)", E_USER_ERROR);
|
user_error("Invalid Response (maybe your calling to wrong URL or server unavailable)", E_USER_WARNING);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user