public function request($url, $data, $header, $method = 'POST') { $logger = JPushLog::getLogger(); $logger->debug("Send " . $method, array("method" => $method, "uri" => $url, "headers" => $header, "body" => $data)); $request = null; if ($method === 'POST') { $request = Request::post($url); } else { if ($method == 'DELETE') { $request = Request::delete($url); } else { $request = Request::get($url); } } if (!is_null($data)) { $request->body($data); } $request->addHeaders($header)->authenticateWith($this->appKey, $this->masterSecret)->timeout(self::READ_TIMEOUT); $response = null; for ($retryTimes = 0;; $retryTimes++) { try { $response = $request->send(); break; } catch (ConnectionErrorException $e) { if (strpos($e->getMessage(), '28')) { throw new APIConnectionException("Response timeout. Your request has probably be received by JPUsh Server,please check that whether need to be pushed again.", true); } if ($retryTimes >= $this->retryTimes) { throw new APIConnectionException("Connect timeout. Please retry later."); } else { $logger->debug("Retry again send " . $method, array("method" => $method, "uri" => $url, "headers" => $header, "body" => $data, "retryTimes" => $retryTimes + 1)); } } } return $response; }
/** * Factory style constructor works nicer for chaining. This * should also really only be used internally. The Request::get, * Request::post syntax is preferred as it is more readable. * @return Request * @param string $method Http Method * @param string $mime Mime Type to Use */ public static function init($method = null, $mime = null) { // Setup our handlers, can call it here as it's idempotent Bootstrap::init(); // Setup the default template if need be if (!isset(self::$_template)) { self::_initializeDefaults(); } $request = new Request(); return $request->_setDefaults()->method($method)->sendsType($mime)->expectsType($mime); }