/** * Recovering suspended tickets. * * @param array $ids * * @return mixed * @throws MissingParametersException * @throws \Zendesk\API\Exceptions\ApiResponseException * @throws \Zendesk\API\Exceptions\RouteException * */ public function recoverMany(array $ids) { if (!is_array($ids)) { throw new MissingParametersException(__METHOD__, ['ids']); } $response = Http::send($this->client, $this->getRoute(__FUNCTION__), ['method' => 'PUT', 'queryParams' => ['ids' => implode(',', $ids)]]); return $response; }
/** * {@inheritdoc} */ public function findAll(array $params = []) { $sideloads = $this->client->getSideload($params); $extraParams = Http::prepareQueryParams($sideloads, $params); $queryParams = array_filter(array_flip($params), [$this, 'filterParams']); $queryParams = array_flip($queryParams); $queryParams = array_merge($queryParams, $extraParams); return $this->traitFindAll($queryParams); }
/** * Show multiple resources * * @param array $ids Array of IDs to delete * @param string $key Could be `id` or `external_id` * * @return mixed * */ public function deleteMany(array $ids = [], $key = 'ids') { try { $route = $this->getRoute(__FUNCTION__); } catch (RouteException $e) { if (!isset($this->resourceName)) { $this->resourceName = $this->getResourceNameFromClass(); } $route = $this->resourceName . '/destroy_many.json'; $this->setRoute('', $route); } $response = Http::send($this->client, $route, ['method' => 'DELETE', 'queryParams' => [$key => implode(',', $ids)]]); $this->client->setSideload(null); return $response; }
/** * Uploads an file with the given upload name. * * @param array $params * * @param string $routeKey * @return null|\stdClass * @throws CustomException * @throws MissingParametersException */ public function upload(array $params, $routeKey = __FUNCTION__) { if (!array_key_exists('file', $params)) { throw new MissingParametersException(__METHOD__, ['file']); } if (!file_exists($params['file'])) { throw new CustomException('File ' . $params['file'] . ' could not be found in ' . __METHOD__); } try { $route = $this->getRoute($routeKey, $params); } catch (RouteException $e) { if (!isset($this->resourceName)) { $this->resourceName = $this->getResourceNameFromClass(); } $this->setRoute(__FUNCTION__, $this->resourceName . '/uploads.json'); $route = $this->resourceName . '/uploads.json'; } $response = Http::send($this->client, $route, ['method' => $this->getUploadRequestMethod(), 'multipart' => [['name' => $this->getUploadName(), 'contents' => new LazyOpenStream($params['file'], 'r'), 'filename' => $params['file']]]]); return $response; }
/** * Update group of resources * * @param array $params * @param string $key Could be `id` or `external_id` * * @return \stdClass | null */ public function updateMany(array $params, $key = 'ids') { try { $route = $this->getRoute(__FUNCTION__); } catch (RouteException $e) { if (!isset($this->resourceName)) { $this->resourceName = $this->getResourceNameFromClass(); } $route = $this->resourceName . '/update_many.json'; $this->setRoute('updateMany', $route); } $resourceUpdateName = $this->objectNamePlural; $queryParams = []; if (isset($params[$key]) && is_array($params[$key])) { $queryParams[$key] = implode(',', $params[$key]); unset($params[$key]); $resourceUpdateName = $this->objectName; } $response = Http::send($this->client, $route, ['queryParams' => $queryParams, 'postFields' => [$resourceUpdateName => $params], 'method' => 'PUT']); $this->client->setSideload(null); return $response; }
/** * Wrapper for common GET requests * * @param $route * @param array $params * * @return \stdClass | null * @throws ResponseException * @throws \Exception */ private function sendGetRequest($route, array $params = []) { $response = Http::send($this->client, $this->getRoute($route, $params), ['queryParams' => $params]); return $response; }
/** * This is a helper method to do a delete request. * * @param $endpoint * * @return null * @throws Exceptions\ApiResponseException */ public function delete($endpoint) { $response = Http::send($this, $endpoint, ['method' => 'DELETE']); return $response; }
/** * Requests autocomplete for users * * @param array $params * * @throws ResponseException * @throws \Exception * @return mixed */ public function autocomplete(array $params) { $response = Http::send($this->client, $this->getRoute(__FUNCTION__), ['method' => 'POST', 'queryParams' => $params]); return $response; }
/** * Delete a resource * * @param $token * * @return bool * @throws MissingParametersException * @throws \Exception * @throws \Zendesk\API\Exceptions\ResponseException */ public function deleteUpload($token) { $response = Http::send($this->client, $this->getRoute(__FUNCTION__, ['token' => $token]), ['method' => 'DELETE']); return $response; }
public function testPutRequestWithContentType() { $response = Http::send($this->client, 'tickets.json', ['check' => 1], 'PUT', 'application/x-www-form-urlencoded'); $data = new \StdClass(); $data->check = 1; $this->assertEquals(is_object($response), true, 'Should return an object'); $this->assertEquals($response->{CURLOPT_URL}, $this->client->getApiUrl() . Http::prepare('tickets.json'), 'Should be the correct url'); $this->assertEquals($response->{CURLOPT_CUSTOMREQUEST}, 'PUT', 'Should be a PUT'); $this->assertEquals($response->{CURLOPT_POSTFIELDS}, $data, 'Should have POST data'); $this->assertContains('Accept: application/json', $response->{CURLOPT_HTTPHEADER}, 'Should contain a Accept header'); $this->assertContains('Content-Type: application/x-www-form-urlencoded', $response->{CURLOPT_HTTPHEADER}, 'Should contain a Content-Type header'); }
/** * This makes a `PUT` request to the endpoint defined by the $callingMethod parameter. * * @param string $callingMethod * @param array $params * * @return array * @throws MissingParametersException * @throws \Exception * @throws \Zendesk\API\Exceptions\ApiResponseException * @throws \Zendesk\API\Exceptions\AuthException */ private function makePutRequest($callingMethod, $params = []) { $this->addUserIdToRouteParams($params); if (isset($params['id'])) { $id = $params['id']; } else { $id = $this->getChainedParameter(self::class); } if (empty($id)) { throw new MissingParametersException(__METHOD__, ['id']); } $response = Http::send($this->client, $this->getRoute($callingMethod, ['id' => $id]), ['method' => 'PUT']); return $response; }
/** * Reorder user fields * * @param array $params * * @return mixed */ public function reorder(array $params) { $postFields = ['user_field_ids' => $params]; $response = Http::send($this->client, $this->getRoute(__FUNCTION__), ['postFields' => $postFields, 'method' => 'PUT']); return $response; }
/** * @param array $params * * @throws MissingParametersException * @throws ResponseException * @return Tickets */ public function merge(array $params = []) { $params = $this->addChainedParametersToParams($params, ['id' => get_class($this)]); if (!$this->hasKeys($params, ['id', 'ids'])) { throw new MissingParametersException(__METHOD__, ['id', 'ids']); } $route = $this->getRoute(__FUNCTION__, ['id' => $params['id']]); unset($params['id']); $response = Http::send($this->client, $route, ['method' => 'POST', 'postFields' => $params]); return $response; }
/** * Seach organizations by external ID * * @param $external_id * @param array $params * * @return array * @throws \Zendesk\API\Exceptions\ApiResponseException * @throws \Zendesk\API\Exceptions\AuthException */ public function search($external_id, array $params = []) { $sideloads = $this->client->getSideload($params); $queryParams = Http::prepareQueryParams($sideloads, $params); $queryParams['external_id'] = $external_id; return $this->client->get($this->getRoute(__FUNCTION__), $queryParams); }
/** * Incremental ticket exports with a supplied start_time * * @param array $params * * @throws MissingParametersException * @throws ResponseException * @throws \Exception * @return mixed */ public function export(array $params) { if (!$params['start_time']) { throw new MissingParametersException(__METHOD__, ['start_time']); } $queryParams = ["start_time" => $params["start_time"]]; $response = Http::send($this->client, $this->getRoute('export'), ["queryParams" => $queryParams]); return $response; }
/** * Export a view * * @param array $params * * @throws MissingParametersException * @throws ResponseException * @throws \Exception * * @return mixed */ public function export(array $params = []) { $params = $this->addChainedParametersToParams($params, ['id' => get_class($this)]); if (!$this->hasKeys($params, ['id'])) { throw new MissingParametersException(__METHOD__, ['id']); } $queryParams = Http::prepareQueryParams($this->client->getSideload($params), $params); return $this->client->get($this->getRoute(__FUNCTION__, ['id' => $params['id']]), $queryParams); }
<?php include "vendor/autoload.php"; use Zendesk\API\Http; use Zendesk\API\HttpClient as ZendeskAPI; $subdomain = "subdomain"; // Your Zendesk subdomain $username = "******"; // Your Zendesk login $oAuthId = "oauth_id"; // The value you entered into the OAuth 'Unique Identifier' field $oAuthSecret = "oauth_secret"; // The OAuth secret given to you by Zendesk $client = new ZendeskAPI($subdomain, $username); if ($_REQUEST['code']) { $response = Http::oauth($client, $_REQUEST['code'], $oAuthId, $oAuthSecret); if ($client->getDebug()->lastResponseCode == 200 && $response->access_token) { echo "<h1>Success!</h1>"; echo "<p>Your OAuth token is: " . $response->access_token . "</p>"; echo "<p>Use this code before any other API call:</p>"; echo "<code><?<br />\$client = new ZendeskAPI(\$subdomain, \$username);<br />\$client->setAuth('oauth_token', '" . $response->access_token . "');<br />?></code>"; } else { echo "<h1>Error!</h1>"; echo "<p>We couldn't get an access token for you. Please check your credentials and try again.</p>"; } } else { echo "<a href=\"https://" . $subdomain . ".zendesk.com/oauth/authorizations/new?response_type=code&redirect_uri=" . ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "&client_id=" . $oAuthId . "&scope=read%20write\">Click to request an OAuth token</a>"; }
/** * Reorder Ticket forms * * @param array $ticketFormIds * * @throws ResponseException * @throws \Exception * @return mixed */ public function reorder(array $ticketFormIds) { $response = Http::send($this->client, $this->getRoute(__FUNCTION__), ['postFields' => ['ticket_form_ids' => $ticketFormIds], 'method' => 'PUT']); return $response; }