Skip to content

Instantly share code, notes, and snippets.

@larscwallin
Created September 7, 2011 08:40
Show Gist options
  • Save larscwallin/1200072 to your computer and use it in GitHub Desktop.
Save larscwallin/1200072 to your computer and use it in GitHub Desktop.
simplx.http
<?php
interface ISimplxHTTPProxy{
public function setBaseURL($url);
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);
public function head($res,$data);
public function trace($res,$data);
public function options($res,$data);
}
class SimplxHTTPClient implements ISimplxHTTPProxy{
protected $username = '';
protected $password = '';
protected $serviceUri = '';
public function setServiceURI($uri){
$this->serviceUri = $uri;
}
public function setBaseURL($url){
$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 'HEAD':
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
break;
case 'TRACE':
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
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;
}
}
}
interface ISimplxRequest{
/*
public function getScheme();
public function setScheme($scheme);
public function getUser();
public function setUser($user);
public function getPassword();
public function setPassword($password);
public function getHostname();
public function setHostname($hostname);
public function getPort();
public function setPort($port);
public function getPath();
public function setPath($path);
public function getQuery();
public function setQuery($query);
public function getTragment();
public function setTragment($fragment);
public $uri;
public $message;
public $headers;
public $resource;
*/
}
interface ISimplxResponse{
/*
public function getRequest();
public function setRequest();
public $uri;
public $message;
public $headers;
public $resource;
*/
}
//parse_url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment