public static function send(Client $client, $endPoint, $data = null, $method = 'GET', $contentType = 'application/json') { $url = $client->getApiUrl() . $endPoint; $method = strtoupper($method); if (null == $data) { $data = new stdClass(); } else { if ($contentType == 'application/json' && $method != 'GET' && $method != 'DELETE') { $data = json_encode($data); } } if ($method == 'POST') { $curl = curl_init($url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); if (is_array($data)) { if (isset($data['upload'])) { $filename = $data['upload']; $file = fopen($filename, 'r'); $size = filesize($filename); $fileData = fread($file, $size); curl_setopt($curl, CURLOPT_POSTFIELDS, $fileData); curl_setopt($curl, CURLOPT_INFILE, $file); curl_setopt($curl, CURLOPT_INFILESIZE, $size); } } } else { if ($method == 'PUT') { $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } else { $url = $url . ($data != null ? (strpos($url, '?') === false ? '?' : '&') . http_build_query($data) : ''); $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method ? $method : 'GET'); } } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_USERPWD, $client->getAuthText()); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: ' . $contentType, 'Accept: application/json')); curl_setopt($curl, CURLINFO_HEADER_OUT, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_VERBOSE, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_MAXREDIRS, 3); $response = curl_exec($curl); if ($response === false) { throw new Exception(sprintf('Curl error message: "%s" in %s', curl_error($curl), __METHOD__)); } $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $responseBody = substr($response, $headerSize); $client->setDebug(curl_getinfo($curl, CURLINFO_HEADER_OUT), curl_getinfo($curl, CURLINFO_HTTP_CODE), substr($response, 0, $headerSize), $responseBody); curl_close($curl); return json_decode($responseBody); }
/** * @param Client $client * @param string $methodName * @param array $extraOptions * @return string in case of any error, an empty string is returned, no warnings generated */ protected function retrieveMethodHelp($client, $methodName, array $extraOptions = array()) { $namespace = '\\PhpXmlRpc\\'; $reqClass = $namespace . 'Request'; $valClass = $namespace . 'Value'; $debug = isset($extraOptions['debug']) ? $extraOptions['debug'] : 0; $timeout = isset($extraOptions['timeout']) ? (int) $extraOptions['timeout'] : 0; $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : ''; $mDesc = ''; $req = new $reqClass('system.methodHelp'); $req->addparam(new $valClass($methodName)); $client->setDebug($debug); $response = $client->send($req, $timeout, $protocol); if (!$response->faultCode()) { $mDesc = $response->value(); if ($client->return_type != 'phpvals') { $mDesc = $mDesc->scalarval(); } } return $mDesc; }
/** * Specific case for OAuth. Run /oauth.php via your browser to get an access token * * @param Client $client * @param string $code * @param string $oAuthId * @param string $oAuthSecret * * @throws \Exception * * @return mixed */ public static function oauth(Client $client, $code, $oAuthId, $oAuthSecret) { $url = 'https://' . $client->getSubdomain() . '.zendesk.com/oauth/tokens'; $method = 'POST'; $curl = curl_init($url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array('grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $oAuthId, 'client_secret' => $oAuthSecret, 'redirect_uri' => ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 'scope' => 'read'))); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($curl, CURLINFO_HEADER_OUT, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_VERBOSE, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_MAXREDIRS, 3); $response = curl_exec($curl); if ($response === false) { throw new \Exception('No response from curl_exec in ' . __METHOD__); } $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $responseBody = substr($response, $headerSize); $client->setDebug(curl_getinfo($curl, CURLINFO_HEADER_OUT), curl_getinfo($curl, CURLINFO_HTTP_CODE), substr($response, 0, $headerSize)); curl_close($curl); return json_decode($responseBody); }
/** * Specific case for OAuth. Run /oauth.php via your browser to get an access token * * @param Client $client * @param string $code * @param string $oAuthId * @param string $oAuthSecret * * @throws \Exception * * @return mixed */ public static function oauth(Client $client, $code, $oAuthId, $oAuthSecret) { $url = 'https://' . $client->getSubdomain() . '.zendesk.com/oauth/tokens'; $method = 'POST'; $curl = isset(self::$curl) ? self::$curl : new CurlRequest(); $curl->setopt(CURLOPT_URL, $url); $curl->setopt(CURLOPT_POST, true); $curl->setopt(CURLOPT_POSTFIELDS, json_encode(array('grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $oAuthId, 'client_secret' => $oAuthSecret, 'redirect_uri' => ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 'scope' => 'read'))); $curl->setopt(CURLOPT_HTTPHEADER, array('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; }