Skip to content

Commit

Permalink
Added HttpException
Browse files Browse the repository at this point in the history
  • Loading branch information
iGusev committed Aug 17, 2015
1 parent 1f9ede3 commit 1203e66
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/BotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,56 @@

class BotApi
{
public static $codes = array(
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',
// Client Error 4xx
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
// Server Error 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
);

/**
* Url prefix
*/
Expand Down Expand Up @@ -83,7 +133,13 @@ public function call($method, array $data = null)

curl_setopt_array($this->curl, $options);

$response = json_decode(curl_exec($this->curl), $this->returnArray);
$result = curl_exec($this->curl);

if(($httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE)) && $httpCode !== 200) {
throw new HttpException(self::$codes[$httpCode], $httpCode);
}

$response = json_decode($result, $this->returnArray);

if ($this->returnArray) {
if (!$response['ok']) {
Expand Down
8 changes: 8 additions & 0 deletions src/HttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace TelegramBot\Api;

class HttpException extends Exception
{

}

0 comments on commit 1203e66

Please sign in to comment.