public function send() { $values = array(); foreach ($this as $prop => $value) { if ($prop != 'endpoint') { $values[$prop] = $value; } } return Beyonic::sendRequest($this->endpoint, 'PUT', $this->id, $values); }
<?php /* Require the lib/Beyonic.php to included to access the interface objects */ require 'lib/Beyonic.php'; /* Set the API Key to be used in all requests */ Beyonic::setApiKey('PLACE_YOUR_KEY_HERE'); /* Show the current callbacks */ echo "**********\n"; echo "Getting All Webhooks\n"; $resp = Beyonic_Webhook::getAll(); $results = $resp['results']; echo 'Get Webhook Response Code: ' . Beyonic::$lastResult['httpResponseCode'] . "\n"; foreach ($results as $index => $hook) { echo "Webhook {$hook->id} calls {$hook->target} on event {$hook->event}.\n"; } echo "**********\n\n"; /* Get a single callback */ echo "**********\n"; echo "Getting Webhook for " . $results[0]->id . "\n"; $hook = Beyonic_Webhook::get($results[0]->id); echo 'Get by Id Webhook Response Code: ' . Beyonic::$lastResult['httpResponseCode'] . "\n"; echo "Webhook {$hook->id} calls {$hook->target} on event {$hook->event}.\n"; echo "**********\n\n"; /* Test Error */ echo "**********\n"; echo "Generating Error by getting Webhook of id 10000\n"; try { $hook = Beyonic_Webhook::get(10000); } catch (Beyonic_Exception $e) { echo "{$e}\n"; echo "{$e->responseBody}\n";
public static function delete($id) { new static(Beyonic::sendRequest(static::$endpoint, 'DELETE', $id)); }
public static function sendRequest($endpoint, $method = 'GET', $id = null, $parameters = null) { $requestURL = self::$apiURL . '/' . $endpoint; if ($id != null) { $requestURL .= '/' . $id; } $jsonData = null; if ($parameters != null) { $metadata = []; foreach ($parameters as $key => $value) { // if the key starts with 'metadata.', transform it to array notation if (strpos($key, 'metadata.') === 0) { $metadata[explode('.', $key)[1]] = $value; unset($parameters[$key]); } } if ($metadata) { $parameters = array_merge($parameters, array('metadata' => $metadata)); } $jsonData = json_encode($parameters); } $httpHeaders = array('Content-Type: application/json', 'Content-Language:en-US', 'Authorization: Token ' . self::$apiKey, 'Beyonic-Client: Php', 'Beyonic-Client-Version: ' . BEYONIC_CLIENT_VERSION); if (self::$apiVersion != null) { $httpHeaders[] = 'Beyonic-Version: ' . self::$apiVersion; } $ch = curl_init(); switch ($method) { case 'GET': if ($parameters != null) { $requestURL .= "?"; foreach ($parameters as $key => $value) { $requestURL .= $key . "=" . urlencode($value) . "&"; } var_dump($requestURL); } break; case 'POST': curl_setopt($ch, CURLOPT_POST, 1); if ($jsonData != null) { curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); $httpHeaders[] = 'Content-Length:' . strlen($jsonData); } break; case 'PATCH': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); if ($jsonData != null) { curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); $httpHeaders[] = 'Content-Length:' . strlen($jsonData); } break; case 'DELETE': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; } curl_setopt($ch, CURLOPT_URL, $requestURL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_VERBOSE, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders); $response = curl_exec($ch); $responseArray = array(); $responseArray['requestURL'] = $requestURL; $responseArray['httpResponseCode'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $responseArray['responseHeader'] = substr($response, 0, $headerSize); $responseArray['responseBody'] = substr($response, $headerSize); self::$lastResult = $responseArray; if ($responseArray['httpResponseCode'] >= 400) { $headerArray = preg_split("/\n/", $responseArray['responseHeader']); throw new Beyonic_Exception(substr($headerArray[0], 0, strlen($headerArray[0]) - 1), $responseArray['httpResponseCode'], $requestURL, $method, $responseArray['responseBody']); } $endpointObject = json_decode($responseArray['responseBody']); return $endpointObject; }