irazasyed / php-transmission-sdk

🔄 PHP Transmission-RPC API SDK with Laravel Support.
MIT License
23 stars 9 forks source link

How to check if remote access is enabled ? #4

Closed vipera7 closed 4 years ago

vipera7 commented 4 years ago

Detailed description

When I'm trying to connect to a remote transmission, I'm getting the following error:

<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-&gt;Transmission\HttpClient\Plugin\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 /php-transmission-sdk/src/HttpClient/Plugin/ExceptionThrower.php(48): Http\Client\Promise\HttpFulfilledPromise-&gt;then(Object(Closure))
#3 /php-transmission-sdk/vendor/php-http/client-common/src/PluginClient.php(132): Transmission\HttpClient\Plugin\ExceptionThrower-&gt;handleRequest(Object(Nyholm\Psr7\Request), O in <b>/php-transmission-sdk/src/Exception/NetworkException.php</b> on line <b>82</b><br />

Possible implementation

I tried something like that (but doesn't work):

require 'vendor/autoload.php';
use Transmission\Exception;

try{
    $transmission = new Transmission\Client($hostname, $port, $username, $password, $httpClientBuilder = null);
} catch (Exception $e) {
    echo "err";
}

I also tried :

if($transmission = new Transmission\Client($hostname, $port, $username, $password, $httpClientBuilder = null)){
    echo "ok";
}else{
    echo "Remote access is diabled";
}

Thanks for any help

irazasyed commented 4 years ago

You can catch the Transmission\Exception\NetworkException instead of just the Exception in your try catch.

vipera7 commented 4 years ago

I did as you said but the issue is still the same :

<?php
require 'vendor/autoload.php';

try{
    $transmission = new Transmission\Client($hostname, $port, $username, $password, $httpClientBuilder = null);
} catch (Transmission\Exception\NetworkException $e) {
    echo "Remote access is diabled";
}
irazasyed commented 4 years ago

What is the error?

vipera7 commented 4 years ago

@irazasyed I'm still getting :

<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-&gt;Transmission\HttpClient\Plugin\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 /php-transmission-sdk/src/HttpClient/Plugin/ExceptionThrower.php(48): Http\Client\Promise\HttpFulfilledPromise-&gt;then(Object(Closure))
#3 /php-transmission-sdk/vendor/php-http/client-common/src/PluginClient.php(132): Transmission\HttpClient\Plugin\ExceptionThrower-&gt;handleRequest(Object(Nyholm\Psr7\Request), O in <b>/php-transmission-sdk/src/Exception/NetworkException.php</b> on line <b>82</b><br />

I made the error (403) for testing purpose.

I also tried with the backslash :

try{
    $transmission = new Transmission\Client($hostname, $port, $username, $password, $httpClientBuilder = null);
} catch (\Transmission\Exception\NetworkException $e) {
    $result = array("Error" => "Remote access is disabled");
}

PHP 7.3.11

irazasyed commented 4 years ago

~I just realized it's a fatal error and you can't catch fatal errors in PHP since it stops the execution of the script. There are workarounds like registering your custom handler in shutdown or log, etc. but otherwise there is no way.~

~You should make sure you have remote access.~

irazasyed commented 4 years ago

Nevermind, I just tested this code and it works fine for me (I'm able to catch the exception).

<?php

require __DIR__.'/vendor/autoload.php';

use Transmission\Client;
use Transmission\Exception\NetworkException;

$hostname = '127.0.0.1';
$port = 9091;
$username = 'username';
$password = 'password';

try{
    $transmission = new Client($hostname, $port, $username, $password);

    $result = $transmission->get(); // Get All Torrents.

    var_dump($result);
} catch (NetworkException $e) {
    echo $e->getMessage();
}
irazasyed commented 4 years ago

Also, in addition to that. In PHP 7 onwards, you can use \Throwable to catch such kinda errors too. So if it still fails for whatever reasons, try that instead. Hope that helps!