我有以下代码:
<?php
require 'vendor/autoload.php';
use Transmission\Exception;
try{
$transmission = new Transmission\Client($hostname, $port, $username, $password, $httpClientBuilder = null);
} catch (NetworkException $e) {
$result = array("Error" => "Remote access is disabled");
}
我正在尝试捕获异常,但仍然出现错误:
<br />
<b>Fatal error</b>: Uncaught Transmission\Exception\NetworkException: 403: Forbidden - Your IP Address is Not Whitelisted in /php-transmission-sdk/src/Exception/NetworkException.php:82
Stack trace:
#0 /php-transmission-sdk/src/HttpClient/Plugin/ExceptionThrower.php(44): Transmission\Exception\NetworkException::createByCode(403, '403: Forbidden ...')
#1 /php-transmission-sdk/vendor/php-http/httplug/src/Promise/HttpFulfilledPromise.php(34): Transmission\HttpClient\Plugin\ExceptionThrower->Transmission\HttpClient\Plugin\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 /php-transmission-sdk/src/HttpClient/Plugin/ExceptionThrower.php(48): Http\Client\Promise\HttpFulfilledPromise->then(Object(Closure))
#3 /php-transmission-sdk/vendor/php-http/client-common/src/PluginClient.php(132): Transmission\HttpClient\Plugin\ExceptionThrower->handleRequest(Object(Nyholm\Psr7\Request), O in <b>/php-transmission-sdk/src/Exception/NetworkException.php</b> on line <b>82</b><br />
NetworkException.php
<?php
namespace Transmission\Exception;
/**
* NetworkException
*/
class NetworkException extends \Exception
{
public const CONFLICT = 409;
public static $statusCodes = [
// 4xx: Client Error - The request contains bad syntax or cannot be fulfilled.
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Payload Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
421 => 'Misdirected Request',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
444 => 'Connection Closed Without Response',
451 => 'Unavailable For Legal Reasons',
499 => 'Client Closed Request',
// 5xx: Server Error - The server failed to fulfill an apparently valid request.
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
508 => 'Loop Detected',
510 => 'Not Extended',
511 => 'Network Authentication Required',
599 => 'Network Connect Timeout Error',
];
/**
* Create Exception by Network Code.
*
* @param int $statusCode
* @param null|string $message
*
* @return static
*/
public static function createByCode(int $statusCode, string $message = null): self
{
$errorMessage = null;
if (isset(static::$statusCodes[$statusCode])) {
$errorMessage = static::$statusCodes[$statusCode];
if (filled($message)) {
$errorMessage = $errorMessage . ' - ' . $message;
}
}
$message = sprintf('%d: %s', $statusCode, $errorMessage ?? $message);
return new static($message, $statusCode);
}
}
回购:https://github.com/irazasyed/php-transmission-sdk
PHP 7.3.11
答案 0 :(得分:0)
您正在使用相对名称空间在catch语句中定义异常Transmission\Exception\NetworkException $e
只需使用NetworkException
或在前面添加反斜杠即可使其成为完全合格的参考。
编辑: 来自PHP文档的参考 https://www.php.net/manual/en/language.namespaces.basics.php
编辑:阐明解决方案
两个
try
{...}
catch (NetworkException $e)
{ ... }
或
try
{...}
catch (\Transmission\Exception\NetworkException $e)
{ ... }
答案 1 :(得分:0)
由于您没有from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
message = 'To be signed'
key = RSA.import_key(open('private_key.der').read())
h = SHA256.new(message)
signature = pkcs1_15.new(key).sign(h)
的{{1}}语句,PHP会假定应该从您当前正在使用的命名空间中加载它。(大概是use
。)要解决此问题,只需添加诸如NetworkException
之类的语句。这将告诉PHP您要捕获的异常类型。
答案 2 :(得分:0)
这是我的解决方法:
<?php
require 'vendor/autoload.php';
try{
$transmission = new Transmission\Client($hostname, $port, $username, $password, $httpClientBuilder = null);
} catch (Transmission\Exception\NetworkException $e) {
$result = array("Error" => "Remote access is disabled");
}