/**
* Execute HTTP Request
*
* @param string $action Action to execute
* @param array|null $data Data to attach to the execution
*
* @return mixed Result of the HTTP Request
*/
public static function execute($action, array $data = null)
{
$debug_handle = TelegramLog::getDebugLogTempStream();
//Fix so that the keyboard markup is a string, not an object
if (isset($data['reply_markup']) && !is_string($data['reply_markup'])) {
$data['reply_markup'] = (string) $data['reply_markup'];
}
$request_params = ['debug' => $debug_handle];
//Check for resources in data
$contains_resource = false;
foreach ($data as $item) {
if (is_resource($item)) {
$contains_resource = true;
break;
}
}
//Reformat data array in multipart way
if ($contains_resource) {
foreach ($data as $key => $item) {
$request_params['multipart'][] = array('name' => $key, 'contents' => $item);
}
} else {
$request_params['form_params'] = $data;
}
try {
$response = self::$client->post('/bot' . self::$telegram->getApiKey() . '/' . $action, $request_params);
} catch (RequestException $e) {
throw new TelegramException($e->getMessage());
} finally {
//Logging verbose debug output
TelegramLog::endDebugLogTempStream("Verbose HTTP Request output:\n%s\n");
}
$result = $response->getBody();
//Logging getUpdates Update
if ($action === 'getUpdates') {
TelegramLog::update($result);
}
return $result;
}