传输php类错误

时间:2015-11-18 22:21:24

标签: php rpc bittorrent transmission torrent

我会使用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);
    } 
}

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

编辑@aergistal&amp; @Miguel:

当我致电test2.php?add时,我得到了这个结果 add
所以,我打电话给test2.php?start=1&amp;我得到了这个结果 enter image description here

但没有结果!!洪流无法启动: enter image description here

$_GET['start']之后的调试:
enter image description here

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
)

2 个答案:

答案 0 :(得分:2)

我认为问题出在 $ _ GET [&#39; start&#39;] id值。检索该值时,您将始终将其检索为字符串

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

add start 方法之间的差异,就是你检索id torrent的方式。虽然添加方法使用torrent api返回的torrent ID(它是整数类型),但您使用的是字符串 启动方法

您可以在发布的调试输出中查看它:

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] 应该是 {&#34; ids&#34;:[1]} 。你可以解决这个将输入id从字符串转换为整数

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

该类应该从字符串转换为整数,但是当你进入类时,有一个方法应该这样做,但做错了。该方法称为 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( 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]替换为字符串值。

答案 1 :(得分:0)

这可能是一种解决方案,但很可能原因更复杂。好吧,我们会看到。
我更改了代码,以便在新添加时暂停新的torrent。所以没有必要阻止它。然后我添加代码来“调试”这个类可以看到的内容,因此使用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:纠正错误错误