php - 传输php类错误

标签 php rpc bittorrent transmission torrent

我会使用this class它允许使用php传输软件,但我不能单独使用这些 Action !

<?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 和@Miguel:

当我调用 test2.php?add 时,我得到这个结果
add
所以,我调用 test2.php?start=1 并得到这个结果
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
)

最佳答案

我认为问题出在 $_GET['start'] id 值中。当您检索该值时,您始终将其作为字符串检索。

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

addstart 方法之间的区别在于检索 id torrent 的方式。虽然Add 方法使用 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 是整数,则[内容]应为{"ids":[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 将按顺序求值。因此,当您从字符串转换为整数时:

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] 替换为字符串值。

关于php - 传输php类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33791575/

相关文章:

php - Xcode 4.1 iOS 引用Joomla generated Database via PHP

php - Zend_Validate_StringLength 编码?

c - Windows 7 中的 rpcgen - 未找到预处理器

testing - 哪些服务更容易测试——REST 或 SOAP RPC 服务?

java - Vaadin 7 从客户端向服务器发送数据

bittorrent DHT 详细规范

PHP - 从查询结果中获取对象数组

php - 如何使用 Doctrine 2 截断表格?

Javascript Torrent 文件解析

php - 创建 torrent 文件的信息哈希时出现问题