Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.21 KB | None | 0 0
  1.     //Issue.php
  2.     /**
  3.      * Create a new issue given an array of $params
  4.      * The issue is assigned to the authenticated user.
  5.      * @link http://www.redmine.org/projects/redmine/wiki/Rest_Issues#Creating-an-issue
  6.      *
  7.      * @param  array             $params the new issue data
  8.      * @return \SimpleXMLElement
  9.      */
  10.     public function create(array $params = array())
  11.     {
  12.         $defaults = array(
  13.             'subject'          => null,
  14.             'description'      => null,
  15.  
  16.             // 'project'          => null,
  17.             // 'category'         => null,
  18.             // 'status'           => null,
  19.             // 'tracker'          => null,
  20.             // 'assigned_to'      => null,
  21.             // 'author'           => null,
  22.  
  23.             'project_id'       => null,
  24.             'category_id'      => null,
  25.             'priority_id'      => null,
  26.             'status_id'        => null,
  27.             'tracker_id'       => null,
  28.             'assigned_to_id'   => null,
  29.             'author_id'        => null,
  30.             'due_date'         => null,
  31.             'start_date'       => null,
  32.             'watcher_user_ids' => null,
  33.         );
  34.         $params = $this->cleanParams($params);
  35.         $params = array_filter(array_merge($defaults, $params));
  36.  
  37.         $xml = $this->buildXML($params);
  38.  
  39.         return $this->post('/issues.xml', $xml->asXML());
  40.         // $json = json_encode(array('issue' => $params));
  41.         // return $this->post('/issues.json', $json);
  42.     }
  43.    
  44.    
  45.     //AbstractApi.php
  46.     /**
  47.      * {@inheritDoc}
  48.      */
  49.     public function post($path, $data)
  50.     {
  51.         return $this->client->post($path, $data);
  52.     }
  53.    
  54.    
  55.     //Client.php
  56.     /**
  57.      * HTTP POSTs $params to $path
  58.      * @param  string $path
  59.      * @param  string $data
  60.      * @return mixed
  61.      */
  62.     public function post($path, $data)
  63.     {
  64.         return $this->runRequest($path, 'POST', $data);
  65.     }
  66.  
  67.     /**
  68.      * @param  string                        $path
  69.      * @param  string                        $method
  70.      * @param  string                        $data
  71.      * @return false|SimpleXMLElement|string
  72.      * @throws \Exception                    If anything goes wrong on curl request
  73.      */
  74.     private function runRequest($path, $method = 'GET', $data = '')
  75.     {
  76.         $this->responseCode = null;
  77.         $this->getPort($this->url.$path);
  78.  
  79.         $curl = curl_init();
  80.         if (isset($this->apikeyOrUsername) && $this->useHttpAuth) {
  81.             if (null === $this->pass) {
  82.                 curl_setopt($curl, CURLOPT_USERPWD, $this->apikeyOrUsername.':'.rand(100000, 199999) );
  83.             } else {
  84.                 curl_setopt($curl, CURLOPT_USERPWD, $this->apikeyOrUsername.':'.$this->pass );
  85.             }
  86.             curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  87.         }
  88.         curl_setopt($curl, CURLOPT_URL, $this->url.$path);
  89.         curl_setopt($curl, CURLOPT_VERBOSE, 0);
  90.         curl_setopt($curl, CURLOPT_HEADER, 0);
  91.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  92.         curl_setopt($curl, CURLOPT_PORT , $this->port);
  93.         if (80 !== $this->port) {
  94.             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->checkSslCertificate);
  95.             curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->checkSslHost);
  96.         }
  97.  
  98.         $tmp = parse_url($this->url.$path);
  99.         $httpHeader = array();
  100.         if ('xml' === substr($tmp['path'], -3)) {
  101.             $httpHeader[] = 'Content-Type: text/xml';
  102.         }
  103.         if ('/uploads.json' === $path || '/uploads.xml' === $path) {
  104.             $httpHeader[] = 'Content-Type: application/octet-stream';
  105.         } elseif ('json' === substr($tmp['path'], -4)) {
  106.             $httpHeader[] = 'Content-Type: application/json';
  107.         }
  108.  
  109.         if (!empty($httpHeader)) {
  110.             if (null === $this->pass) {
  111.                 $httpHeader[] = 'X-Redmine-API-Key: '.$this->apikeyOrUsername;
  112.             }
  113.             curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeader);
  114.         }
  115.  
  116.         switch ($method) {
  117.             case 'POST':
  118.                 curl_setopt($curl, CURLOPT_POST, 1);
  119.                 if (isset($data)) {curl_setopt($curl, CURLOPT_POSTFIELDS, $data);}
  120.                 break;
  121.             case 'PUT':
  122.                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
  123.                 if (isset($data)) {curl_setopt($curl, CURLOPT_POSTFIELDS, $data);}
  124.                 break;
  125.             case 'DELETE':
  126.                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
  127.                 break;
  128.             default: // GET
  129.                 break;
  130.         }
  131.         $response = curl_exec($curl);
  132.         $this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  133.  
  134.         if (curl_errno($curl)) {
  135.             $e = new \Exception(curl_error($curl), curl_errno($curl));
  136.             curl_close($curl);
  137.             throw $e;
  138.         }
  139.         curl_close($curl);
  140.  
  141.         if ($response) {
  142.             // if response is XML, return an SimpleXMLElement object
  143.             if ('<' === substr($response, 0, 1)) {
  144.                 return new \SimpleXMLElement($response);
  145.             }
  146.  
  147.             return $response;
  148.         }
  149.  
  150.         return true;
  151.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement