Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Issue.php
- /**
- * Create a new issue given an array of $params
- * The issue is assigned to the authenticated user.
- * @link http://www.redmine.org/projects/redmine/wiki/Rest_Issues#Creating-an-issue
- *
- * @param array $params the new issue data
- * @return \SimpleXMLElement
- */
- public function create(array $params = array())
- {
- $defaults = array(
- 'subject' => null,
- 'description' => null,
- // 'project' => null,
- // 'category' => null,
- // 'status' => null,
- // 'tracker' => null,
- // 'assigned_to' => null,
- // 'author' => null,
- 'project_id' => null,
- 'category_id' => null,
- 'priority_id' => null,
- 'status_id' => null,
- 'tracker_id' => null,
- 'assigned_to_id' => null,
- 'author_id' => null,
- 'due_date' => null,
- 'start_date' => null,
- 'watcher_user_ids' => null,
- );
- $params = $this->cleanParams($params);
- $params = array_filter(array_merge($defaults, $params));
- $xml = $this->buildXML($params);
- return $this->post('/issues.xml', $xml->asXML());
- // $json = json_encode(array('issue' => $params));
- // return $this->post('/issues.json', $json);
- }
- //AbstractApi.php
- /**
- * {@inheritDoc}
- */
- public function post($path, $data)
- {
- return $this->client->post($path, $data);
- }
- //Client.php
- /**
- * HTTP POSTs $params to $path
- * @param string $path
- * @param string $data
- * @return mixed
- */
- public function post($path, $data)
- {
- return $this->runRequest($path, 'POST', $data);
- }
- /**
- * @param string $path
- * @param string $method
- * @param string $data
- * @return false|SimpleXMLElement|string
- * @throws \Exception If anything goes wrong on curl request
- */
- private function runRequest($path, $method = 'GET', $data = '')
- {
- $this->responseCode = null;
- $this->getPort($this->url.$path);
- $curl = curl_init();
- if (isset($this->apikeyOrUsername) && $this->useHttpAuth) {
- if (null === $this->pass) {
- curl_setopt($curl, CURLOPT_USERPWD, $this->apikeyOrUsername.':'.rand(100000, 199999) );
- } else {
- curl_setopt($curl, CURLOPT_USERPWD, $this->apikeyOrUsername.':'.$this->pass );
- }
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- }
- curl_setopt($curl, CURLOPT_URL, $this->url.$path);
- curl_setopt($curl, CURLOPT_VERBOSE, 0);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_PORT , $this->port);
- if (80 !== $this->port) {
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->checkSslCertificate);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->checkSslHost);
- }
- $tmp = parse_url($this->url.$path);
- $httpHeader = array();
- if ('xml' === substr($tmp['path'], -3)) {
- $httpHeader[] = 'Content-Type: text/xml';
- }
- if ('/uploads.json' === $path || '/uploads.xml' === $path) {
- $httpHeader[] = 'Content-Type: application/octet-stream';
- } elseif ('json' === substr($tmp['path'], -4)) {
- $httpHeader[] = 'Content-Type: application/json';
- }
- if (!empty($httpHeader)) {
- if (null === $this->pass) {
- $httpHeader[] = 'X-Redmine-API-Key: '.$this->apikeyOrUsername;
- }
- curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeader);
- }
- switch ($method) {
- case 'POST':
- curl_setopt($curl, CURLOPT_POST, 1);
- if (isset($data)) {curl_setopt($curl, CURLOPT_POSTFIELDS, $data);}
- break;
- case 'PUT':
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
- if (isset($data)) {curl_setopt($curl, CURLOPT_POSTFIELDS, $data);}
- break;
- case 'DELETE':
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
- break;
- default: // GET
- break;
- }
- $response = curl_exec($curl);
- $this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if (curl_errno($curl)) {
- $e = new \Exception(curl_error($curl), curl_errno($curl));
- curl_close($curl);
- throw $e;
- }
- curl_close($curl);
- if ($response) {
- // if response is XML, return an SimpleXMLElement object
- if ('<' === substr($response, 0, 1)) {
- return new \SimpleXMLElement($response);
- }
- return $response;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement