传输phpclass错误

Transmission php class error

我会使用this class,它允许使用带有php的传输软件,但我不能单独使用这些操作!

<?php

require_once( dirname( __FILE__ ) . '/TransmissionRPC.class.php' );

$test_torrent = "http://www.slackware.com/torrents/slackware64-13.1-install-dvd.torrent";

$rpc = new TransmissionRPC();
$rpc->sstats( );

if (isset($_GET['add']))
{
    try
    { 

      $result = $rpc->add( $test_torrent, '/tmp' );
      $id = $result->arguments->torrent_added->id;
      print "ADD TORRENT TEST... [{$result->result}] (id=$id)\n";
      sleep( 2 );

      $rpc->stop( $id );


    } catch (Exception $e) {
      die('[ERROR] ' . $e->getMessage() . PHP_EOL);
    } 
}

if (isset($_GET['start']))
{
    try
    {  
      $rpc->start( $_GET['start'] );

    } catch (Exception $e) {
      die('[ERROR] ' . $e->getMessage() . PHP_EOL);
    } 
}

添加种子后的第一个动作运行(停止种子)但我无法重新启动....

为@aergistal 和@Miguel 编辑:

当我调用 test2.php?add 时,我得到了这个结果

所以,我调用 test2.php?start=1 并得到这个结果

但是没有结果!!洪流不启动:

$_GET['start']后调试:

TRANSMISSIONRPC_DEBUG:: request( method=torrent-start, ...):: Stream context created with options:
Array
(
    [http] => Array
        (
            [user_agent] => TransmissionRPC for PHP/0.3
            [ignore_errors] => 1
            [method] => POST
            [header] => Content-type: application/json
X-Transmission-Session-Id: 4C3KBYhu79SVvFcXrrG4RmpFLZaGu54RSLHT0hFqeVEmAmlV

            [content] => {"method":"torrent-start","arguments":{"ids":["1"]}}
        )

)
TRANSMISSIONRPC_DEBUG:: request( method=torrent-start, ...):: POST Result: 
{"arguments":{},"result":"success"}
TRANSMISSIONRPC_DEBUG:: request( method=torrent-start, ...):: Stream meta info: 
Array
(
    [wrapper_data] => Array
        (
            [0] => HTTP/1.0 200 OK
            [1] => Server: Transmission
            [2] => Content-Type: application/json; charset=UTF-8
        )

    [wrapper_type] => http
    [stream_type] => tcp_socket/ssl
    [mode] => r
    [unread_bytes] => 0
    [seekable] => 
    [uri] => http://localhost:9091/transmission/rpc
    [timed_out] => 
    [blocked] => 1
    [eof] => 1
)

这可能是一个解决方案,但很可能原因更复杂。好吧,我们拭目以待。
我更改了代码,以便在新添加时暂停新的种子。所以没有必要阻止它。然后我将代码添加到 "debug" 这个 class 可以看到的内容,所以用 test2.php?list=1 调用你的网站应该打印它拥有的种子。奇怪的是,我看不出有任何理由说明原始代码不起作用。

<?php
require_once(dirname(__FILE__) . '/TransmissionRPC.class.php');

$test_torrent = "http://www.slackware.com/torrents/slackware64-13.1-install-dvd.torrent";

$rpc = new TransmissionRPC();
$rpc->sstats();

if (isset($_GET['add'])) {
    try {

        $result = $rpc->add_file($test_torrent, '/tmp', array('paused' => true));
        $id     = $result->arguments->torrent_added->id;
        print "ADD TORRENT TEST... [{$result->result}] (id=$id)\n";
        //sleep(2);
        //$rpc->stop($id);

    } catch (Exception $e) {
        die('[ERROR] ' . $e->getMessage() . PHP_EOL);
    }
}

if (isset($_GET['start'])) {
    try {
        echo '<pre>';
        print_r($rpc->start($_GET['start']));
        echo '</pre>';         

    } catch (Exception $e) {
        die('[ERROR] ' . $e->getMessage() . PHP_EOL);
    }
}

//might be interesting to check what torrents you have
if (isset($_GET['list'])) {
    try {
        echo '<pre>';
        print_r($rpc->get());
        echo '</pre>';         
    } catch (Exception $e) {
        die('[ERROR] ' . $e->getMessage() . PHP_EOL);
    }
}

来自 atmoner:更正错误 bug

我认为问题出在 $_GET['start'] id 值中。当您检索值时,您将始终检索它 as String.

// for index.php?start=1
var_dump($_GET['start'); // will ouput string(1) "1"

addstart 方法的不同之处在于您检索 id 种子的方式。虽然 Add 方法 使用种子 api 返回的种子 ID(它是 整数类型 ),但您使用的是 string for start method.

您可以在您发布的调试输出中进行检查:

TRANSMISSIONRPC_DEBUG:: request( method=torrent-start, ...):: 使用选项创建的流上下文:

Array
(
    [http] => Array
        (
            [user_agent] => TransmissionRPC for PHP/0.3
            [ignore_errors] => 1
            [method] => POST
            [header] => Content-type: application/json
X-Transmission-Session-Id: 4C3KBYhu79SVvFcXrrG4RmpFLZaGu54RSLHT0hFqeVEmAmlV

            [content] => {"method":"torrent-start","arguments":{"ids":["1"]}}
        )

)

如果您传递的 id 是整数,则 [content] 应该是 {"ids":[1]} .您可以解决此问题将输入 ID 从字符串转换为整数

奖金:为什么在 TransmissionRPC.class.php 中投射不起作用?

class 应该从字符串转换为整数,但是当您进入 class 内部时,有一种方法应该可以做到这一点,但做错了。该方法称为 cleanRequestData.

这是代码:

  protected function cleanRequestData ( $array )
  {
    if ( !is_array( $array ) || count( $array ) == 0 ) return null; // Nothing to clean
    setlocale( LC_NUMERIC, 'en_US.utf8' );  // Override the locale - if the system locale is wrong, then 12.34 will encode as 12,34 which is invalid JSON
    foreach ( $array as $index => $value )
    {
      if( is_object( $value ) ) $array[$index] = $value->toArray(); // Convert objects to arrays so they can be JSON encoded
      if( is_array( $value ) ) $array[$index] = $this->cleanRequestData( $value );  // Recursion
      if( empty( $value ) && $value != 0 ) unset( $array[$index] ); // Remove empty members
      if( is_numeric( $value ) ) $array[$index] = $value+0; // Force type-casting for proper JSON encoding (+0 is a cheap way to maintain int/float/etc)
      if( is_bool( $value ) ) $array[$index] = ( $value ? 1 : 0);   // Store boolean values as 0 or 1
      if( is_string( $value ) ) $array[$index] = utf8_encode( $value ); // Make sure all data is UTF-8 encoded for Transmission
    }
    return $array;
  }

乍一看似乎还不错,但如果仔细观察细节,就会发现整个 IF 将按顺序求值。因此,当您从字符串转换为整数时:

if( is_numeric( $value ) ) $array[$index] = $value+0;   // Force type-casting for proper JSON encoding (+0 is a cheap way to maintain int/float/etc)

修改数组不是 $value 变量。所以,这意味着当你评估 $value 是否是一个字符串时,它当然是:

if( is_string( $value ) ) $array[$index] = utf8_encode( $value );   // Make sure all data is UTF-8 encoded for Transmission

所以你在这里输入,然后它将一个整数转换数组元素 $array[$index] 替换为一个字符串值。