Php 传输类错误
我会使用它允许使用php的传输软件,但我不能单独使用的行动Php 传输类错误,php,rpc,bittorrent,transmission,torrent,Php,Rpc,Bittorrent,Transmission,Torrent,我会使用它允许使用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( );
<?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。所以没有必要阻止它。然后我将代码添加到这个类可以看到的“debug”中,因此使用
test2.php?list=1调用您的网站应该可以打印出它的torrents。奇怪的是,我不明白为什么原始代码不能工作
<?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);
}
}
我认为问题出在$\u GET['start']id值上。检索值时,始终将其作为字符串检索
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;
}
add和start方法的不同之处在于检索id torrent的方式。Add方法使用torrent api返回的torrent id(它是一个整数类型),而start方法使用的是字符串
您可以在发布的调试输出中检查它:
TRANSMISSIONRPC_DEBUG::request(方法=torrent start…):使用选项创建的流上下文:
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)
如果您传递的id是整数,[content]应该是{“ids”:[1]}。您可以解决将输入id从字符串转换为整数的问题
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;
}
好处:为什么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_string( $value ) ) $array[$index] = utf8_encode( $value ); // Make sure all data is UTF-8 encoded for Transmission
第一次看这个似乎很好,但是如果你看一下细节,你可以看到整个if都将被依次评估。因此,当您从字符串强制转换为整数时:
修改数组,而不是$value变量。因此,这意味着当您计算$value是否为字符串时,它当然是:
因此,您在此处输入,然后将整型数组元素$array[$index]替换为字符串值。再次显示错误消息是什么?无错误。。。没有错误返回!该流在传输中不启动。但是如果我在停止后运行start,它就会工作!只是启动操作不起作用。当isset($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\post@Miguel我已经为$\u GET['list']
更新了我的帖子+1,但是你的代码并不能解决我的问题!它只允许我知道目前在我的客户端使用的洪流,我需要启动和停止洪流时,我想现在开始吗?不!呼叫时无启动?启动=[id]
奇怪,“成功”消息实际上来自哪里@在周一3R我更新了代码,所以我们得到了一些关于开始的信息。。有希望地。请发布或评论其输出。更新结果:但torrent未启动!:/我已经三天没有试着去理解为什么这不起作用了,我总是在这里出现这个结果哦!怎么说。。。。太完美了!!如果可以的话,我会给你比100分多得多的分数=)一点耐心和我给你的奖励,就在我做测试的时候,我很高兴能帮助你:-)
if( is_string( $value ) ) $array[$index] = utf8_encode( $value ); // Make sure all data is UTF-8 encoded for Transmission