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); } $result = curl_exec($ch); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if(in_array($status_code,[200,201,204])){ return json_decode($result); }else{ throw new \Exception("API $status_code Code: ".$result); } } }