PHP模块用于读取torrent文件

Bri*_*n G 17 php bittorrent

是否有一个PHP模块,您可以使用它以编程方式读取torrent以查找有关它的信息,例如Seeders?

Svi*_*ish 19

我曾经在一个小网站上使用过这些功能.想想我发现他们用一个名为OpenTracker的php bittorrent跟踪器或其他东西,但找不到网站......

你不会在torrent文件中找到播种机.torrent文件只包含有关文件,哈希码和长度等的信息.我相信一些跟踪器信息.你需要从跟踪器中获得多少个播种机等.您可以在BitTorrent.org上阅读有关协议的信息.我认为,这种沟通方式是良好的,所以你也可以使用这些功能.这意味着你只需弄清楚要发送什么来获得你想要的东西.

注意:我没有写这三个功能.就像我说的,我发现它们是一个开源洪流跟踪器的源头.这些函数没有被注释,但函数名称和一个print_r一起在torrent文件的结果上你知道信息应该足以理解如何使用它们.我在底部添加了一些示例代码来说明我是如何使用它们的.他们工作了.

function bdecode($str) {
    $pos = 0;
    return bdecode_r($str, $pos);
}

function bdecode_r($str, &$pos) {
    $strlen = strlen($str);
    if (($pos < 0) || ($pos >= $strlen)) {
            return null;
    }
    else if ($str{$pos} == 'i') {
            $pos++;
            $numlen = strspn($str, '-0123456789', $pos);
            $spos = $pos;
            $pos += $numlen;
            if (($pos >= $strlen) || ($str{$pos} != 'e')) {
                    return null;
            }
            else {
                    $pos++;
                    return intval(substr($str, $spos, $numlen));
            }
    }
    else if ($str{$pos} == 'd') {
            $pos++;
            $ret = array();
            while ($pos < $strlen) {
                    if ($str{$pos} == 'e') {
                            $pos++;
                            return $ret;
                    }
                    else {
                            $key = bdecode_r($str, $pos);
                            if ($key == null) {
                                    return null;
                            }
                            else {
                                    $val = bdecode_r($str, $pos);
                                    if ($val == null) {
                                            return null;
                                    }
                                    else if (!is_array($key)) {
                                            $ret[$key] = $val;
                                    }
                            }
                    }
            }
            return null;
    }
    else if ($str{$pos} == 'l') {
            $pos++;
            $ret = array();
            while ($pos < $strlen) {
                    if ($str{$pos} == 'e') {
                            $pos++;
                            return $ret;
                    }
                    else {
                            $val = bdecode_r($str, $pos);
                            if ($val == null) {
                                    return null;
                            }
                            else {
                                    $ret[] = $val;
                            }
                    }
            }
            return null;
    }
    else {
            $numlen = strspn($str, '0123456789', $pos);
            $spos = $pos;
            $pos += $numlen;
            if (($pos >= $strlen) || ($str{$pos} != ':')) {
                    return null;
            }
            else {
                    $vallen = intval(substr($str, $spos, $numlen));
                    $pos++;
                    $val = substr($str, $pos, $vallen);
                    if (strlen($val) != $vallen) {
                            return null;
                    }
                    else {
                            $pos += $vallen;
                            return $val;
                    }
            }
    }
}

function bencode($var) {
    if (is_int($var)) {
            return 'i' . $var . 'e';
    }
    else if (is_array($var)) {
            if (count($var) == 0) {
                    return 'de';
            }
            else {
                    $assoc = false;
                    foreach ($var as $key => $val) {
                            if (!is_int($key)) {
                                    $assoc = true;
                                    break;
                            }
                    }
                    if ($assoc) {
                            ksort($var, SORT_REGULAR);
                            $ret = 'd';
                            foreach ($var as $key => $val) {
                                    $ret .= bencode($key) . bencode($val);
                            }
                            return $ret . 'e';
                    }
                    else {
                            $ret = 'l';
                            foreach ($var as $val) {
                                    $ret .= bencode($val);
                            }
                            return $ret . 'e';
                    }
            }
    }
    else {
            return strlen($var) . ':' . $var;
    }
}
Run Code Online (Sandbox Code Playgroud)

一些示例用法:

# Read a file
$content = file_get_contents("file.torrent");
$content_d = bdecode($content);

# Check if bdecode succeeded
if(empty($content_d)) exit('Something is wrong with the torrent. BDecode failed.');

# Calculate info_hash
$info_hash = sha1(bencode($content_d['info']), true);

# Calculate length
$length = 0;
function add_length($value, $key)
{
    global $length;
    if($key == 'length') $length += $value;
}
array_walk_recursive($content_d, 'add_length');
Run Code Online (Sandbox Code Playgroud)

  • php编程员听说过评论吗? (3认同)
  • 好问题。但这不是我的代码,就像我说的那样。我不评论其他人的代码。因此,请不要因为其他人的不良习惯而把我否决。。。 (2认同)

Era*_*rin 8

Google 在sourceforge 上提供了这个PHP客户端,在PHP类上提供了这个torrent类.应该是你所需要的一切.


Con*_*tin 7

Torrent文件基本上是用BEncode编码的嵌套字典.BEncode是一个简单的编码,有一些BDecode PHP类,就像这个.

BEP0003中描述了torrent文件的结构.

请注意,torrent文件不包含您提到的"Seeders"字段.播种者列表是动态的,由跟踪服务器管理.拥有torrent hash_infotracker_url(都可以从torrent文件获得)你可以向跟踪器发送scrape-request,它将返回'完整'字段中的播种者数量,参见Tracker Scrape Convention.