本文整理汇总了PHP中Exception::create方法的典型用法代码示例。如果您正苦于以下问题:PHP Exception::create方法的具体用法?PHP Exception::create怎么用?PHP Exception::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Exception
的用法示例。
在下文中一共展示了Exception::create方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: request
function request($method, $url, $body = NULL, $header = array())
{
if (is_array($body)) {
if (!empty($body)) {
$body = json_encode($body);
array_push($header, "Content-Type: application/json");
} else {
$body = NULL;
}
}
$request = curl_init();
curl_setopt_array($request, $this->options);
$url = strtolower(substr($url, 0, 6)) == "https:" ? $url : self::API_ENDPOINT . $url;
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, strtoupper($method));
if (count($header) > 0) {
curl_setopt($request, CURLOPT_HTTPHEADER, $header);
}
if ($body) {
curl_setopt($request, CURLOPT_POSTFIELDS, $body);
}
$response = curl_exec($request);
if (is_string($response)) {
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE);
curl_close($request);
$headers = self::parseHeaders(substr($response, 0, $headerSize));
$body = substr($response, $headerSize);
if (isset($headers["compression-count"])) {
Tinify::setCompressionCount(intval($headers["compression-count"]));
}
$isJson = false;
if (isset($headers["content-type"])) {
/* Parse JSON response bodies. */
list($contentType) = explode(";", $headers["content-type"], 2);
if (strtolower(trim($contentType)) == "application/json") {
$isJson = true;
}
}
/* 1xx and 3xx are unexpected and will be treated as error. */
$isError = $status <= 199 || $status >= 300;
if ($isJson || $isError) {
/* Parse JSON bodies, always interpret errors as JSON. */
$body = json_decode($body);
if (!$body) {
$message = sprintf("Error while parsing response: %s (#%d)", PHP_VERSION_ID >= 50500 ? json_last_error_msg() : "Error", json_last_error());
throw Exception::create($message, "ParseError", $status);
}
}
if ($isError) {
throw Exception::create($body->message, $body->error, $status);
}
return (object) array("body" => $body, "headers" => $headers);
} else {
$message = sprintf("%s (#%d)", curl_error($request), curl_errno($request));
curl_close($request);
throw new ConnectionException("Error while connecting: " . $message);
}
}
示例2: request
function request($method, $url, $body = NULL, $header = array())
{
if (is_array($body)) {
if (!empty($body)) {
$body = json_encode($body);
array_push($header, "Content-Type: application/json");
} else {
$body = NULL;
}
}
$request = curl_init();
curl_setopt_array($request, $this->options);
$url = strtolower(substr($url, 0, 6)) == "https:" ? $url : Client::API_ENDPOINT . $url;
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_HTTPHEADER, $header);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, strtoupper($method));
if ($body) {
curl_setopt($request, CURLOPT_POSTFIELDS, $body);
}
$response = curl_exec($request);
if (is_string($response)) {
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE);
curl_close($request);
$headers = self::parseHeaders(substr($response, 0, $headerSize));
$body = substr($response, $headerSize);
if (isset($headers["compression-count"])) {
Tinify::setCompressionCount(intval($headers["compression-count"]));
}
if ($status >= 200 && $status <= 299) {
return array("body" => $body, "headers" => $headers);
}
$details = json_decode($body);
if (!$details) {
$message = sprintf("Error while parsing response: %s (#%d)", PHP_VERSION_ID >= 50500 ? json_last_error_msg() : "Error", json_last_error());
$details = (object) array("message" => $message, "error" => "ParseError");
}
throw Exception::create($details->message, $details->error, $status);
} else {
$message = sprintf("%s (#%d)", curl_error($request), curl_errno($request));
curl_close($request);
throw new ConnectionException("Error while connecting: " . $message);
}
}
示例3: initImports
/**
* Versucht alle Klasen, die importiert werden müssen wenn die Klasse geschrieben würde zu ermitteln
*
* dies geht natürlich nicht immer komplett fehlerfrei (aber erstmal ein schöner Anfang)
*
* @param ClassReader $classReader wird der ClassReader übergeben, werden aus diesem die Imports übernommen
* @return array
*/
public function initImports(GClass $gClass, ClassReader $classReader = NULL)
{
/* Wo haben wir imports?
- Funktions-Parameter als Hints
- in Bodys von Funktionen (können wir nicht)
- beim extend oder implement (es ist höchstwahrscheinlich, dass wenn wir ein interface implementieren wir da auch imports haben)
- die GClass selbst hat in usedClasses einen Array von Schlüssel FQN und Wert array(GClass, alias)
*/
/* classReader */
if (isset($classReader)) {
if (!$classReader->getClass()->equals($gClass)) {
throw Exception::create("Die Klasse des ClassReaders ist eine andere als die angegebene! '%s' != '%s' ", $classReader->getClass()->getFQN(), $gClass->getFQN());
}
$this->foundImports = $classReader->readUseStatements();
} else {
// start empty
$this->foundImports = array();
}
/* zuerst die Parameter */
foreach ($gClass->getMethods() as $method) {
foreach ($method->getParameters() as $parameter) {
if (($hint = $parameter->getHint()) instanceof GClass) {
$this->addImport($hint);
}
}
}
/* implement */
foreach ($gClass->getInterfaces() as $interface) {
$this->addImport($interface);
}
/* die Classes aus der GClass */
foreach ($gClass->getUsedClasses() as $list) {
list($class, $alias) = $list;
$this->addImport($class, $alias);
}
return $this;
}