Код из одного класса первого шага, чтобы поговорить с ubuntuOne
<?php
class ubuntuOne{
var $curl = array('cookieSrc'=>'cookie.txt','enableCookies'=>false);
var $auth = array('consumer_key'=>false,'consumer_secret'=>false,'token'=>false,'token_secret'=>false);
var $oauth = false;
function ubuntuOne(){
}
function u1_getRoot(){
if($this->oauth === false){return false;}
$url = 'https://one.ubuntu.com/api/file_storage/v1';
$this->oauth->fetch($url);
print_r($this->oauth->getLastResponse());
}
function u1_listFolder($path){
if($this->oauth === false){return false;}
//FIXME: parse $path
$url = 'https://one.ubuntu.com/api/file_storage/v1';
//FIXME: $path debe terminar en '/'
$url .= str_replace(' ','%20',$path);
$this->oauth->fetch($url);
$lr = $this->oauth->getLastResponse();
if($lr === '{"error": "not found"}'){return false;}
print_r($this->oauth->getLastResponse());
}
function u1_createFolder($name,$path = ''){
//FIXME: folder exists?
$url = 'https://one.ubuntu.com/api/file_storage/v1';
//FIXME: $path debe terminar en '/'
$url .= str_replace(' ','%20',$path);
//FIXME: $name no puede contener '/'
$url .= str_replace(' ','%20',$name);
$this->oauth->fetch($url,'{"kind":"directory"}',OAUTH_HTTP_METHOD_PUT);
print_r($this->oauth->getLastResponse());
}
function u1_file_exists($path){
//FIXME: cache?
$url = 'https://one.ubuntu.com/api/file_storage/v1';
$url .= str_replace(' ','%20',$path);
try{$this->oauth->fetch($url);}
catch(OAuthException $E){if($E->lastResponse === '{"error": "not found"}'){return false;}}
$i = $this->oauth->getLastResponseInfo();
if($i['http_code'] === 200){}
print_r($this->oauth->getLastResponseInfo());
}
function requestAuthentification($user,$pass,$name){
$url = 'https://login.ubuntu.com/api/1.0/authentications?ws.op=authenticate&token_name=Ubuntu%20One%20@%20'.rawurlencode($name);
$data = curlPetition(array('URL'=>$url,'USERPWD'=>$user.':'.$pass));
//FIXME: check the response header -> 200
$this->auth = json_decode($data['responseBody'],1);
}
function registerToken($user){
$url = 'https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/'.$user;
$this->oauth->fetch($url);
$r = $this->oauth->getLastResponse();
if(substr($r,02) !== 'ok'){
//FIXME: poner error
}
print_r($this->oauth->getLastResponse());
}
function saveAuth($fileName){$ar = fopen($fileName,'w');fwrite($ar,json_encode($this->auth));fclose($ar);return true;}
function loadAuth($fileName){
if(!file_exists($fileName)){return false;}
$this->auth = json_decode(file_get_contents($fileName),1);
if($this->auth === NULL){return false;}
$this->oauth = new OAuth($this->auth['consumer_key'],$this->auth['consumer_secret'],OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$this->oauth->enableDebug();
$this->oauth->enableSSLChecks();
$this->oauth->setToken($this->auth['token'],$this->auth['token_secret']);
return true;
}
function curlPetition($arr,$data = array()){
$curl = curl_init($arr['URL']);
if(!isset($data['URLTRACK'])){$data['URLTRACK'] = array();}
$data['URLTRACK'][] = $arr['URL'];
if(isset($this->curl['userAgent'])){curl_setopt($curl,CURLOPT_USERAGENT,$this->curl['userAgent']);}
if(count($data['URLTRACK']) > 1){curl_setopt($curl,CURLOPT_REFERER,$data['URLTRACK'][count($data['URLTRACK'])-2]);}
if(isset($arr['USERPWD'])){curl_setopt($curl,CURLOPT_USERPWD,$arr['USERPWD']);}
if(isset($arr['userAgent'])){curl_setopt($curl,CURLOPT_USERAGENT,$arr['userAgent']);}
if(isset($arr['POST'])){curl_setopt($curl,CURLOPT_POST,true);curl_setopt($curl,CURLOPT_POSTFIELDS,$arr['POST']);}
if(isset($arr['referer'])){curl_setopt($curl,CURLOPT_REFERER,$arr['referer']);}
if(isset($arr['timeout'])){curl_setopt($curl,CURLOPT_TIMEOUT,$arr['timeout']);}
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_HEADER,true);
if($this->curl['enableCookies'] !== false ){$cookieSrc = $this->curl['cookieSrc'];curl_setopt($curl,CURLOPT_COOKIEFILE,$cookieSrc);curl_setopt($curl,CURLOPT_COOKIEJAR,$cookieSrc);}
if(isset($arr['header'])){curl_setopt($curl,CURLOPT_HTTPHEADER,$arr['header']);}
curl_setopt($curl,CURLOPT_TIMEOUT,25);
$viewcode = curl_exec($curl);
$curlInfo = curl_getinfo($curl);
curl_close($curl);
if(empty($viewcode)){return false;}
$data['responseHeader'] = substr($viewcode,0,$curlInfo['header_size']);
$data['responseBody'] = substr($viewcode,$curlInfo['header_size']);
//$data['viewcode'] = $viewcode;
if(isset($arr['FOLLOWLOCATION']) && preg_match('/HTTP\/1\.1 30[12]{1}/',$data['responseHeader'])){
preg_match('/Location: (.*)/',$data['responseHeader'],$p);
$nurl = trim($p[1]);
if($nurl[0]=='/'){list($arr['URL'],) = explode('/',str_replace('http://','',$arr['URL']));$nurl = 'http://'.$arr['URL'].$nurl;}
$arr['URL'] = $nurl;
unset($arr['POST']);
return curlPetition($arr,$data);
}
return $data;
}
}
?>
Некоторые примеры вызовов (srry для расстройства и комментария кода, возможно, документация в один прекрасный день):
echo time()."\n";
$ub = new ubuntuOne;
/* The first time you made the commented calls, then you save the authorization
* to a file. Once you have it on a file, you load it every time from there */
//$ub->requestAuthentification('sombra2eternity@gmail.com','*****','st');
//$ub->registerToken('sombra2eternity@gmail.com');
//print_r($ub->auth);
//$ub->saveAuth($GLOBALS['userPath'].'db/uOne.protected');
$ub->loadAuth($GLOBALS['userPath'].'db/uOne.protected');
//$ub->registerToken('sombra2eternity@gmail.com');
$ub->u1_getRoot();
//$ub->u1_file_exists('/~/Ubuntu One/non_exists/');
echo "\n";
$ub->u1_listFolder('/~/Ubuntu One/');
echo "\n";
$ub->u1_createFolder('new folder','/~/Ubuntu One/');
Удачи, надеюсь, это поможет