Created
August 26, 2011 15:31
-
-
Save larscwallin/1173673 to your computer and use it in GitHub Desktop.
SimplxRESTClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface ISimplxRestProxy{ | |
public function setServiceURI($uri); | |
public function login($usn,$psw); | |
public function logout(); | |
public function get($res,$data); | |
public function post($res,$data); | |
public function patch($res,$data); | |
public function put($res,$data); | |
public function delete($res,$data); | |
} | |
class SimplxRESTClient implements ISimplxRestProxy{ | |
protected $username = ''; | |
protected $password = ''; | |
protected $serviceUri = ''; | |
public function setServiceURI($uri){ | |
$this->serviceUri = $uri; | |
} | |
public function login($usn,$psw){ | |
if($usn && $psw){ | |
$this->username = $usn; | |
$this->password = $psw; | |
}else{ | |
return false; | |
} | |
} | |
public function logout(){ | |
$this->username = ''; | |
$this->password = ''; | |
} | |
public function get($res,$data){ | |
return $this->execute($res,$data,'GET'); | |
} | |
public function post($res,$data){ | |
return $this->execute($res,$data,'POST'); | |
} | |
public function patch($uri,$data){ | |
return $this->execute($res,$data,'POST'); | |
} | |
public function put($res,$data){ | |
return $this->execute($res,$data,'PUT'); | |
} | |
public function delete($res,$data){ | |
return $this->execute($res,$data,'DELETE'); | |
} | |
private function execute($res,$data,$method = 'GET'){ | |
global $modx; | |
if ($this->serviceUri != '' && $res != '') { | |
$ch = curl_init(); | |
// Use basic auth only if both username and password are provided | |
if($this->username && $this->password){ | |
curl_setopt($ch, CURLOPT_USERPWD, ($this->username.':'.$this->password)); | |
} | |
curl_setopt($ch,CURLOPT_URL,$this->serviceUri.$res); | |
switch($method){ | |
case 'POST': | |
curl_setopt($ch,CURLOPT_POST,1); | |
curl_setopt($ch,CURLOPT_POSTFIELDS,$data); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
break; | |
case 'PATCH': | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); | |
break; | |
case 'DELETE': | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
break; | |
case 'PUT': | |
/* | |
curl_setopt($ch, CURLOPT_VERBOSE, 1); | |
curl_setopt($ch, CURLOPT_PUT, 1); | |
curl_setopt($ch, CURLOPT_INFILE, $fp); | |
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile)); | |
*/ | |
break; | |
default: | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
} | |
$output = curl_exec($ch); | |
curl_close($ch); | |
return $output; | |
}else{ | |
return false; | |
} | |
} | |
} | |
/* $test = new SimplxRESTClient(); | |
$test->setServiceURI('http://80.70.2.84/sodexo/'); | |
$test->get('?id=26&orderNumber=ORD201100064',''); | |
*/ | |
interface ISimplxRequest{ | |
/* | |
public $scheme; | |
public $userinfo; | |
public $hostname; | |
public $port; | |
public $path; | |
public $query; | |
public $fragment; | |
public $uri; | |
public $message; | |
public $headers; | |
public $resource; | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment