我正在使用我下载的课程;由于对 php 类和助手不太熟悉,我决定发布它,看看是否有更多经验的人可以为我遇到的错误提供一些帮助。
我正在使用这个调用脚本“torrenttest”中的函数
<?php
require_once 'Torrentmaker.php';
$torrent = new Torrent( 'file', 'http://fr33dom.h33t.com:3310/announce' );
$torrent->announce(array('udp://tracker.openbittorrent.com:80/announce',
if ( ! $error = $torrent->error() ) // error method return the last error message
$torrent->save('test2.torrent'); // save to disk
else
echo '<br>DEBUG: ',$error;
问题是当“文件”在同一台服务器上时它工作正常;但是,当它调用远程服务器时,它会给出超时错误,或者只是创建一个没有大小的 torrent 文件(无法运行)
这是 Torrentmaker.php 的脚本
class Torrent {
public function __construct ( $data = null, $meta = array(), $piece_length = 256 ) {
if ( is_null( $data ) )
return false;
if ( $piece_length < 32 || $piece_length > 4096 )
return self::set_error( new Exception( 'Invalid piece lenth, must be between 32 and 4096' ) );
if ( is_string( $meta ) )
$meta = array( 'announce' => $meta );
if ( $this->build( $data, $piece_length * 1024 ) )
$this->touch();
else
$meta = array_merge( $meta, $this->decode( $data ) );
foreach( $meta as $key => $value )
$this->{$key} = $value;
public function content ( $precision = null ) {
$files = array();
if ( isset( $this->info['files'] ) && is_array( $this->info['files'] ) )
foreach ( $this->info['files'] as $file )
$files[self::path( $file['path'], $this->info['name'] )] = $precision ?
self::format( $file['length'], $precision ) :
$file['length'];
elseif ( isset( $this->info['name'] ) )
$files[$this->info['name']] = $precision ?
self::format( $this->info['length'], $precision ) :
$this->info['length'];
return $files;
}
public function save ( $filename = null ) {
return file_put_contents( is_null( $filename ) ? $this->info['name'] . '.torrent' : $filename, $this->encode( $this ) );
}
static public function file_get_contents ( $file, $timeout = self::timeout, $offset = null, $length = null ) {
if ( is_file( $file ) || ini_get( 'allow_url_fopen' ) ) {
$context = ! is_file( $file ) && $timeout ?
stream_context_create( array( 'http' => array( 'timeout' => $timeout ) ) ) :
null;
return ! is_null( $offset ) ? $length ?
@file_get_contents( $file, false, $context, $offset, $length ) :
@file_get_contents( $file, false, $context, $offset ) :
@file_get_contents( $file, false, $context );
} elseif ( ! function_exists( 'curl_init' ) )
return self::set_error( new Exception( 'Install CURL or enable "allow_url_fopen"' ) );
$handle = curl_init( $file );
if ( $timeout )
curl_setopt( $handle, CURLOPT_TIMEOUT, $timeout );
if ( $offset || $length )
curl_setopt( $handle, CURLOPT_RANGE, $offset . '-' . ( $length ? $offset + $length -1 : null ) );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 1 );
$content = curl_exec( $handle );
$size = curl_getinfo( $handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD );
curl_close( $handle );
return ( $offset && $size == -1 ) || ( $length && $length != $size ) ? $length ?
substr( $content, $offset, $length) :
substr( $content, $offset) :
$content;
public function errors() {
return empty( self::$errors ) ?
false :
self::$errors;
}
}