/** * Perform the Api call * * @param type $options * @return type * @throws Exception */ function api($options = []) { $start = microtime(true); $options[CURLOPT_USERNAME] = $this->username; $options[CURLOPT_PASSWORD] = $this->password; $options[CURLOPT_FAILONERROR] = false; $options[CURLOPT_RETURNTRANSFER] = true; $options += Curl::$defaults; $response = new Curl($options); $body = $response->getBody(); $this->logger->append($options[CURLOPT_URL], ['relativeUrl' => substr($options[CURLOPT_URL], strlen($this->baseUrl)), 'duration' => microtime(true) - $start]); if (in_array($response->http_code, [200, 201])) { return Json::decode($body); } if (substr($body, 0, 1) === '{') { // looks like json? $json = json_decode($body); if (is_object($json) && isset($json->errorMessages) && count($json->errorMessages) > 0) { throw new Exception('[Jira] ' . $json->errorMessages[0]); } if (is_object($json) && isset($json->errors) && is_object($json->errors)) { throw new Exception('[Jira] ' . current(get_object_vars($json->errors))); } } throw new Exception('[Jira] ' . $response->http_code . ' ' . $options[CURLOPT_URL]); }
public function test_put() { $filename = \Sledgehammer\TMP_DIR . basename(__CLASS__) . '.txt'; file_put_contents($filename, 'Curl TEST'); $request = Curl::putFile('http://date.jsontest.com/?service=ip', $filename, [CURLOPT_FAILONERROR => false]); $this->assertSame(405, $request->http_code); }
/** * Start the request * 1. Set the CURLOPT_* options * 2. Add the request to the pool * 3. Starts the request. * * @param array $options CURLOPT_* options * * @throws Exception */ private function start($options) { $this->state = 'ERROR'; self::$requests[] = $this; // Watch changes // Setting options foreach ($options as $option => $value) { if (curl_setopt($this->handle, $option, $value) === false) { throw new Exception('Setting option:' . self::optionName($option) . ' failed'); } $option = self::optionName($option); $this->options[$option] = $value; } // multi curl init if (self::$pool === null) { self::$pool = curl_multi_init(); if (self::$pool === false) { throw new Exception('Failed to create cURL multi handle'); } if (self::$keepalive) { register_shutdown_function(function () { Curl::$keepalive = false; // Close the multi handle when all requests are completed. Curl::synchronize(); }); } } // Wait until a new tranfer can be added. self::throttle(self::$maxConcurrent - 1); // Add request $error = curl_multi_add_handle(self::$pool, $this->handle); while ($error === CURLM_CALL_MULTI_PERFORM) { $error = curl_multi_exec(self::$pool, $active); } if ($error !== CURLM_OK) { throw new Exception('[' . self::multiErrorName($error) . '] Failed to add cURL handle'); } ++self::$tranferCount; // Start request do { $error = curl_multi_exec(self::$pool, $active); } while ($error === CURLM_CALL_MULTI_PERFORM); if ($error !== CURLM_OK) { throw new Exception('[' . self::multiErrorName($error) . '] Failed to execute cURL multi handle'); } $this->state = 'RUNNING'; }