GiteaBot/src/Client.php
2019-06-25 09:15:51 +01:00

51 lines
1.4 KiB
PHP

<?php
class Client{
public function __construct($url,$user,$pass,$debug=false){
$this->url=$url;
$this->user=$user;
$this->pass=$pass;
$this->debug=$debug;
}
public function getRepo($user,$repo){
return new Repo($this,$user,$repo);
}
public function request($url, $type='GET', $postFields = null){
if(substr($url,0,4)!='http'){
$url=$this->url.$url;
}
if($this->debug){
echo ">> $type $url ".json_encode($postFields)." <<\n";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'james/GiteaBot');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if($type!='GET'){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
}
if (!is_null($postFields)) {
$postFields = json_encode($postFields);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json',
'Content-Length: '.strlen($postFields), ]);
}
if($this->user) {
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
}
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
}