TelegramBotApiWrapper::apiRequestJson()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.439
cc 5
eloc 22
nc 6
nop 2
1
<?php
2
3
namespace JarvisPHP\TelegramBot;
4
5
/**
6
 * TelegramBot
7
 * @author Many
8
 * copied from example found in Telegram site 
9
 */
10
class TelegramBotApiWrapper {
11
12
	function exec_curl_request($handle) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
	  $response = curl_exec($handle);
14
15
	  if ($response === false) {
16
	    $errno = curl_errno($handle);
17
	    $error = curl_error($handle);
18
	    echo("Curl returned error $errno: $error\n");
19
	    curl_close($handle);
20
	    return false;
21
	  }
22
23
	  $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
24
	  curl_close($handle);
25
26
	  if ($http_code >= 500) {
27
	    // do not want to DDOS server if something goes wrong
28
	    sleep(10);
29
	    return false;
30
	  } else if ($http_code != 200) {
31
	    $response = json_decode($response, false);
32
	    echo("Request has failed with error ".$response->error_code." : ".$response->description."\n");
33
	    if ($http_code == 401) {
34
	      throw new Exception('Invalid access token provided');
35
	    }
36
	    return false;
37
	  } else {
38
	    $response = json_decode($response, false);
39
	    if (isset($response->description)) {
40
	      echo("Request was successfull: ".$response->description."\n");
41
	    }
42
	    $response = $response->result;
43
	  }
44
45
	  return $response;
46
	}
47
48
	function apiRequestJson($method, $parameters) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
49
	  if (!is_string($method)) {
50
	    echo("Method name must be a string\n");
51
	    return false;
52
	  }
53
54
	  if (!$parameters) {
55
	    $parameters = array();
56
	  } else if (!is_array($parameters)) {
57
	    error_log("Parameters must be an array\n");
58
	    return false;
59
	  }
60
61
	  $parameters["method"] = $method;
62
63
	  $_BOT_TOKEN = '';
64
	  //Load API key from json config
65
      if(file_exists('TelegramBot/api-key.json')) {
66
          //Create your own bot token and put it in api-key.json
67
          // like {"bot_token": "<your-bot-token>"}
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
          $json_config = json_decode(file_get_contents('TelegramBot/api-key.json'));
69
          $_BOT_TOKEN = $json_config->bot_token;
70
      }
71
72
	  $handle = curl_init('https://api.telegram.org/bot'.$_BOT_TOKEN.'/');
73
	  curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
74
	  curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
75
	  curl_setopt($handle, CURLOPT_TIMEOUT, 60);
76
	  curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
77
	  curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
78
	  curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
79
80
	  return $this->exec_curl_request($handle);
81
	}
82
83
}