/** * Create service * * @param ServiceLocatorInterface $serviceLocator * @return Client */ public function createService(ServiceLocatorInterface $serviceLocator) { $config = $serviceLocator->get('Configuration'); $client = new Client($config['zendesk']['subdomain'], $config['zendesk']['username']); $client->setAuth('basic', ['username' => $config['zendesk']['username'], 'token' => $config['zendesk']['token']]); return $client; }
/** * Register the service provider. */ public function register() { $this->app->bind('zendesk', function () { $client = new ZendeskAPI($this->app['config']->get('services.zendesk.subdomain'), $this->app['config']->get('services.zendesk.username')); $client->setAuth('basic', array('username' => $this->app['config']->get('services.zendesk.username'), 'token' => $this->app['config']->get('services.zendesk.token'))); return $client; }); }
/** * Sets up the fixture, for example, open a network connection. * This method is called before a test is executed. */ protected function setUp() { $this->client = new HttpClient($this->subdomain, $this->username, $this->scheme, $this->hostname, $this->port); $authOptions['username'] = $this->username; if ($this->authStrategy === 'basic') { $authOptions['token'] = $this->token; } else { $authOptions['token'] = $this->oAuthToken; } $this->client->setAuth($this->authStrategy, $authOptions); }
/** * This checks the response with the given index * * @param $options * @param int $index */ public function assertRequestIs($options, $index = 0) { $this->assertArrayHasKey($index, $this->mockedTransactionsContainer, 'Should have made an API call.'); $transaction = $this->mockedTransactionsContainer[$index]; $request = $transaction['request']; $response = $transaction['response']; $options = array_merge(['statusCode' => 200, 'headers' => ['Accept' => 'application/json', 'Content-Type' => 'application/json']], $options); $this->assertEquals($options['statusCode'], $response->getStatusCode()); if (isset($options['multipart'])) { $body = $request->getBody(); $this->assertInstanceOf(MultipartStream::class, $body); $this->assertGreaterThan(0, $body->getSize()); $this->assertNotEmpty($header = $request->getHeaderLine('Content-Type')); $this->assertContains('multipart/form-data', $header); unset($options['headers']['Content-Type']); } if (isset($options['file'])) { $body = $request->getBody(); $this->assertInstanceOf(LazyOpenStream::class, $body); $this->assertGreaterThan(0, $body->getSize()); $this->assertEquals($options['file'], $body->getMetadata('uri')); $this->assertNotEmpty($header = $request->getHeaderLine('Content-Type')); $this->assertEquals('application/binary', $header); unset($options['headers']['Content-Type']); } if (isset($options['headers']) && is_array($options['headers'])) { foreach ($options['headers'] as $headerKey => $value) { if ($value) { $this->assertNotEmpty($header = $request->getHeaderLine($headerKey)); $this->assertEquals($value, $header); } } } if (isset($options['method'])) { $this->assertEquals($options['method'], $request->getMethod()); } if (isset($options['endpoint'])) { // Truncate the base path from the target $apiBasePath = "/{$this->client->getApiBasePath()}"; $endpoint = preg_replace('/^' . preg_quote($apiBasePath, '/') . '/', '', $request->getUri()->getPath()); $this->assertEquals($options['endpoint'], $endpoint); } if (isset($options['queryParams'])) { $expectedQueryParams = urldecode(http_build_query($options['queryParams'])); $this->assertEquals($expectedQueryParams, $request->getUri()->getQuery()); } if (isset($options['postFields'])) { $this->assertEquals(json_encode($options['postFields']), $request->getBody()->getContents()); } if (isset($options['requestUri'])) { $this->assertEquals($options['requestUri'], $request->getUri()->__toString()); } }
/** * Use the send method to call every endpoint except for oauth/tokens * * @param HttpClient $client * @param string $endPoint E.g. "/tickets.json" * @param array $options * Available options are listed below: * array $queryParams Array of unencoded key-value pairs, e.g. ["ids" => "1,2,3,4"] * array $postFields Array of unencoded key-value pairs, e.g. ["filename" => "blah.png"] * string $method "GET", "POST", etc. Default is GET. * string $contentType Default is "application/json" * * @return array The response body, parsed from JSON into an associative array * @throws ApiResponseException * @throws AuthException */ public static function send(HttpClient $client, $endPoint, $options = []) { $options = array_merge(['method' => 'GET', 'contentType' => 'application/json', 'postFields' => null, 'queryParams' => null], $options); $headers = ['Accept' => 'application/json', 'Content-Type' => $options['contentType'], 'User-Agent' => $client->getUserAgent()]; $request = new Request($options['method'], $client->getApiUrl() . $endPoint, $headers); $requestOptions = []; if (!empty($options['multipart'])) { $request = $request->withoutHeader('Content-Type'); $requestOptions['multipart'] = $options['multipart']; } elseif (!empty($options['postFields'])) { $request = $request->withBody(\GuzzleHttp\Psr7\stream_for(json_encode($options['postFields']))); } elseif (!empty($options['file'])) { if (is_file($options['file'])) { $fileStream = new LazyOpenStream($options['file'], 'r'); $request = $request->withBody($fileStream); } } if (!empty($options['queryParams'])) { foreach ($options['queryParams'] as $queryKey => $queryValue) { $uri = $request->getUri(); $uri = $uri->withQueryValue($uri, $queryKey, $queryValue); $request = $request->withUri($uri, true); } } try { list($request, $requestOptions) = $client->getAuth()->prepareRequest($request, $requestOptions); $response = $client->guzzle->send($request, $requestOptions); } catch (RequestException $e) { throw new ApiResponseException($e); } finally { $client->setDebug($request->getHeaders(), $request->getBody()->getContents(), isset($response) ? $response->getStatusCode() : null, isset($response) ? $response->getHeaders() : null, isset($e) ? $e : null); $request->getBody()->rewind(); } if (isset($file)) { fclose($file); } $client->setSideload(null); return json_decode($response->getBody()->getContents()); }
<?php include "../../vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; /** * Replace the following with your own. */ $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; $client = new ZendeskAPI($subdomain); $client->setAuth('basic', ['username' => $username, 'token' => $token]); try { // Search the current customer $params = array('query' => '*****@*****.**'); $search = $client->users()->search($params); if (empty($search->users)) { echo "This email address could not be found on Zendesk"; } else { foreach ($search->users as $UserData) { $UserId = $UserData->id; $tickets = $client->users($UserId)->requests()->findAll(); // Show the results echo "<pre>"; print_r($tickets); echo "</pre>"; } } } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo 'Please check your credentials. Make sure to change the $subdomain, $username, and $token variables in this file.'; }
/** * Specific case for OAuth. Run /oauth.php via your browser to get an access token * * @param HttpClient $client * @param string $code * @param string $oAuthId * @param string $oAuthSecret * * @throws \Exception * @return mixed */ public static function oauth(HttpClient $client, $code, $oAuthId, $oAuthSecret) { $url = 'https://' . $client->getSubdomain() . '.zendesk.com/oauth/tokens'; $protocol = $_SERVER['HTTPS'] ? 'https://' : 'http://'; $curl = isset(self::$curl) ? self::$curl : new CurlRequest(); $curl->setopt(CURLOPT_URL, $url); $curl->setopt(CURLOPT_POST, true); $curl->setopt(CURLOPT_POSTFIELDS, json_encode(['grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $oAuthId, 'client_secret' => $oAuthSecret, 'redirect_uri' => $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 'scope' => 'read'])); $curl->setopt(CURLOPT_HTTPHEADER, ['Content-Type: application/json']); $curl->setopt(CURLINFO_HEADER_OUT, true); $curl->setopt(CURLOPT_RETURNTRANSFER, true); $curl->setopt(CURLOPT_CONNECTTIMEOUT, 30); $curl->setopt(CURLOPT_TIMEOUT, 30); $curl->setopt(CURLOPT_SSL_VERIFYPEER, false); $curl->setopt(CURLOPT_HEADER, true); $curl->setopt(CURLOPT_VERBOSE, true); $curl->setopt(CURLOPT_FOLLOWLOCATION, true); $curl->setopt(CURLOPT_MAXREDIRS, 3); $response = $curl->exec(); if ($response === false) { throw new \Exception(sprintf('Curl error message: "%s" in %s', $curl->error(), __METHOD__)); } $headerSize = $curl->getinfo(CURLINFO_HEADER_SIZE); $responseBody = substr($response, $headerSize); $responseObject = json_decode($responseBody); $client->setDebug($curl->getinfo(CURLINFO_HEADER_OUT), $curl->getinfo(CURLINFO_HTTP_CODE), substr($response, 0, $headerSize), isset($responseObject->error) ? $responseObject : null); $curl->close(); self::$curl = null; return $responseObject; }
<?php include "../../vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; /** * Replace the following with your own. */ $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; $client = new ZendeskAPI($subdomain); $client->setAuth('basic', ['username' => $username, 'token' => $token]); try { // Search the current customer $params = array('query' => '*****@*****.**'); $search = $client->users()->search($params); // verify if this email address exists if (empty($search->users)) { echo 'This email adress could not be found on Zendesk.'; } else { foreach ($search->users as $UserData) { echo "<pre>"; print_r($UserData); echo "</pre>"; } } } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo 'Please check your credentials. Make sure to change the $subdomain, $username, and $token variables in this file.'; }
<?php include "../../vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; /** * Replace the following with your own. */ $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; $client = new ZendeskAPI($subdomain); $client->setAuth('basic', ['username' => $username, 'token' => $token]); try { // Update a new ticket $updateTicket = $client->tickets()->update(41, ['priority' => 'urgent', 'comment' => ['body' => 'We have changed your ticket priority to Urgent and will keep you up-to-date asap.']]); // Show result echo "<pre>"; print_r($updateTicket); echo "</pre>"; } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo 'Please check your credentials. Make sure to change the $subdomain, $username, and $token variables in this file.'; }
<?php include "../../vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; /** * Replace the following with your own. */ $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; $client = new ZendeskAPI($subdomain); $client->setAuth('basic', ['username' => $username, 'token' => $token]); try { // Query Zendesk API to retrieve the ticket details $id = 31; $tickets = $client->tickets()->find($id); // Show the results echo "<pre>"; print_r($tickets->ticket); echo "</pre>"; } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo 'Please check your credentials. Make sure to change the $subdomain, $username, and $token variables in this file.'; }
<?php // load Composer require 'vendor/autoload.php'; use Zendesk\API\HttpClient as ZendeskAPI; $subdomain = "z3nrafaelcabreraromero"; $username = "******"; $token = "Jv5ZX9TTquXAg3umv6Od9VyWr68lKEirq4sSvX2m"; // replace this with your token $client = new ZendeskAPI($subdomain, $username); $client->setAuth('basic', ['username' => $username, 'token' => $token]);
<?php include "../../vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; /** * Replace the following with your own. */ $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; $client = new ZendeskAPI($subdomain); $client->setAuth('basic', ['username' => $username, 'token' => $token]); try { // Delete a ticket by id $id = '51'; $deleteTicket = $client->tickets()->delete($id); echo "Ticket ({$id}) has been removed"; } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo 'Please check your credentials. Make sure to change the $subdomain, $username, and $token variables in this file.'; }
<?php include "../../vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; /** * Replace the following with your own. */ $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; $client = new ZendeskAPI($subdomain); $client->setAuth('basic', ['username' => $username, 'token' => $token]); try { $query = $client->groups()->findAll(); foreach ($query as $UserData) { echo "<pre>"; print_r($UserData); echo "</pre>"; } } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo 'Please check your credentials. Make sure to change the $subdomain, $username, and $token variables in this file.'; }
<?php include "vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; // replace this with your token //$password = "******"; $client = new ZendeskAPI($subdomain, $username); $client->setAuth('token', $token); // set either token or password // Get all tickets $tickets = $client->tickets()->findAll(); print_r($tickets); // Create a new ticket $newTicket = $client->tickets()->create(array('subject' => 'The quick brown fox jumps over the lazy dog', 'comment' => array('body' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'), 'priority' => 'normal')); print_r($newTicket); // Update multiple tickets $client->ticket(array(123, 456))->update(array('status' => 'urgent')); // Delete a ticket $client->ticket(123)->delete();
<?php include "../vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; /** * Replace the following with your own. */ $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; $client = new ZendeskAPI($subdomain, $username); $client->setAuth('basic', ['username' => $username, 'token' => $token]); try { // Get all tickets $tickets = $client->tickets()->findAll(); // Create a new ticket $newTicket = $client->tickets()->create(array('subject' => 'The quick brown fox jumps over the lazy dog', 'comment' => array('body' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'), 'priority' => 'normal')); print_r($tickets); print_r($newTicket); } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo 'Please check your credentials. Make sure to change the $subdomain, $username, and $token variables in this file.'; }
<?php include "../../vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; /** * Replace the following with your own. */ $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; $client = new ZendeskAPI($subdomain); $client->setAuth('basic', ['username' => $username, 'token' => $token]); try { $query = $client->users()->create(['name' => 'API Demo', 'email' => '*****@*****.**', 'phone' => '+1-954-704-6031', 'role' => 'end-user', 'details' => 'This user has been created with the API.']); echo "<pre>"; print_r($query); echo "</pre>"; } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo 'Please check your credentials. Make sure to change the $subdomain, $username, and $token variables in this file.'; }
<?php include "../../vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; /** * Replace the following with your own. */ $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; $client = new ZendeskAPI($subdomain); $client->setAuth('basic', ['username' => $username, 'token' => $token]); try { $newGroup = $client->groups()->create(array('name' => '2d line')); // Show result echo "<pre>"; print_r($newGroup); echo "</pre>"; } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo 'Please check your credentials. Make sure to change the $subdomain, $username, and $token variables in this file.'; }
/** * Enable side-loading (beta) - flags until the next chain * * @param array $fields * * @return $this */ public function sideload(array $fields = []) { $this->client->setSideload($fields); return $this; }
/** * Sets up the fixture, for example, open a network connection. * This method is called before a test is executed. */ protected function setUp() { $this->client = new HttpClient($this->subdomain, $this->username, $this->scheme, $this->hostname, $this->port); $this->client->setAuth('oauth', ['token' => $this->oAuthToken]); }
<?php include "../../vendor/autoload.php"; use Zendesk\API\HttpClient as ZendeskAPI; /** * Replace the following with your own. */ $subdomain = "subdomain"; $username = "******"; $token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; $client = new ZendeskAPI($subdomain); $client->setAuth('basic', ['username' => $username, 'token' => $token]); try { // Create a new ticket wi $newTicket = $client->tickets()->create(array('type' => 'problem', 'tags' => array('demo', 'testing', 'api', 'zendesk'), 'subject' => 'The quick brown fox jumps over the lazy dog', 'comment' => array('body' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'), 'requester' => array('locale_id' => '1', 'name' => 'Example User', 'email' => '*****@*****.**'), 'priority' => 'normal')); // Show result echo "<pre>"; print_r($newTicket); echo "</pre>"; } catch (\Zendesk\API\Exceptions\ApiResponseException $e) { echo 'Please check your credentials. Make sure to change the $subdomain, $username, and $token variables in this file.'; }