www.webdeveloper.com
Results 1 to 5 of 5

Thread: Class PHP

  1. #1
    Join Date
    Apr 2014
    Posts
    5

    Class PHP

    I try this

    Code:
    <?php
    
    $meuObjeto = new Torrent();
    
    $meuObjeto->scrape("$r"); 
    
    ?>

    and this

    Code:
    <?php
    
    $meuObjeto = new Torrent();
    
    $meuObjeto->scrape("udp://fopen.demonii.com", 7C90CFEE93DE8C4FA04526DAA5CE530ADFB8DF6E); 
    
    ?>

    not sucess...

  2. #2
    Join Date
    Apr 2014
    Posts
    5
    How use this class ?

    PHP Code:
    <?php
     
    class Torrent
    {
        public function 
    scrape(array $announce = [ ] , $hash_info null)
        {
     
            
    $r = [
                
    'seeders'  => ,
                
    'leechers' => 0
            
    ];
     
            foreach ( 
    $announce as $an ) {
     
                
    $d get_remote_peers($an $hash_info);
     
                if ( isset(
    $d['seeders']) ) {
                    
    $r['seeders'] += $d['seeders'];
                }
     
                if ( isset(
    $d['leechers']) ) {
                    
    $r['leechers'] += $d['leechers'];
                }
            }
     
            return 
    $r;
        }
     
    }
     
     
    /**
     * Gets amout of remote tracker peers. May be recursivity.
     *
     * @param string $url Announce url of request
     * @param string $info_hash Info-hash of torrent to be parsed
     * @param string $method Method of gathering amount of peers. May be scrape or announce. Default 'scrape'. If scrape fails, recursivety swithes to announce and executes again.
     *
     * @return array Result array ('tracker','seeders','leechers','state');
     */
    function get_remote_peers($url $info_hash $method 'scrape')
    {
     
        if ( 
    $method == "announce" ) {
            
    $get_params = array(
                
    "info_hash"  => pack("H*" $info_hash) ,
                
    "peer_id"    => "-UT1820-5dmPcUOYGnrx" ,
                
    "port"       => rand(10000 65535) ,
                
    "uploaded"   => ,
                
    "no_peer_id" => ,
                
    "downloaded" => ,
                
    "compact"    => ,
                
    "left"       => ,
                
    "numwant"    => 9999
            
    );
        } else {
            
    $urlorig    $url;
            
    $url        str_replace('announce' 'scrape' $url);
            
    $get_params = array(
                
    "info_hash" => pack("H*" $info_hash)
            );
        }
     
     
        
    $urlInfo   = @parse_url($url);
        
    $http_host $urlInfo['host'];
        
    $http_port getUrlPort($urlInfo);
        
    $scheme    $urlInfo['scheme'];
     
        if ( 
    $http_port === ) {
            return array( 
    'tracker' => $http_host 'state' => 'failed:no_port_detected' 'method' => "$scheme:$method'remote_method' => 'N/A' );
        }
     
        
    $http_path          $urlInfo['path'];
        
    $get_request_params = @explode('&' $urlInfo['query']);
     
        
    $new_get_request_params false;
        foreach ( 
    array_filter($get_request_params) as $array_value ) {
            list(
    $key $value) = explode('=' $array_value);
            
    $new_get_request_params[$key] = $value;
        }
     
        if ( !
    $new_get_request_params ) {
            
    $new_get_request_params = array();
        }
        
    // Params gathering complete
     
        // Creating params
        
    $http_params = @http_build_query(@array_merge($new_get_request_params $get_params));
        
    $opts        = array(
            
    $scheme => array(
                
    'method'  => 'GET' ,
                
    'header'  => 'User-Agent: uTorrent/1820' ,
                
    'timeout' => 3
                
    //'Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2',
            
    )
        );
    Last edited by NogDog; 04-21-2014 at 03:31 PM. Reason: changed code tag to php tag and added closing tag

  3. #3
    Join Date
    Apr 2014
    Posts
    5
    PHP Code:
    $req_uri $scheme '://' $http_host ':' $http_port $http_path . ($http_params '?' $http_params '');
     
     
        if ( 
    $scheme == 'udp' && $method == 'scrape' ) {
     
            
    $transaction_id mt_rand(65535);
            
    $fp             = @fsockopen($scheme '://' $http_host $http_port $errno $errstr); //sockets only
            
    if ( !$fp ) {
                return array( 
    'tracker' => $http_host 'state' => 'failed:timeout' 'method' => "$scheme:$method'remote_method' => 'socket' );
            }
            
    stream_set_timeout($fp 3);
     
            
    $current_connid "\x00\x00\x04\x17\x27\x10\x19\x80";
     
            
    //Connection request
            
    $packet $current_connid pack("N" 0) . pack("N" $transaction_id);
            
    fwrite($fp $packet);
     
            
    //Connection response
            
    $ret fread($fp 16);
            if ( 
    strlen($ret) < ) {
                return 
    get_remote_peers($urlorig $info_hash "announce");
            }
            if ( 
    strlen($ret) < 16 ) {
                return array( 
    'tracker' => $http_host 'state' => 'failed:invalid_udp_packet' 'method' => "$scheme:$method'remote_method' => 'socket' );
            }
            
    $retd unpack("Naction/Ntransid" $ret);
            if ( 
    $retd['action'] != || $retd['transid'] != $transaction_id ) {
                return array( 
    'tracker' => $http_host 'state' => 'failed:invalid_udp_response' 'method' => "$scheme:$method'remote_method' => 'socket' );
            }
            
    $current_connid substr($ret 8);
     
            
    //Scrape request
            
    $packet $current_connid pack("N" 2) . pack("N" $transaction_id) . pack('H*' $info_hash);
            
    fwrite($fp $packet);
            
    //Scrape response
            
    $readlength 20//8 + (12);
            
    $ret        fread($fp $readlength);
            if ( 
    strlen($ret) < ) {
                return 
    get_remote_peers($urlorig $info_hash "announce");
            }
            if ( 
    strlen($ret) < ) {
                array( 
    'tracker' => $http_host 'state' => 'failed:invalid_udp_packet' 'method' => "$scheme:$method'remote_method' => 'socket' );
            }
            
    $retd unpack("Naction/Ntransid" $ret);
            
    // Todo check for error string if response = 3
            
    if ( $retd['action'] != || $retd['transid'] != $transaction_id ) {
                array( 
    'tracker' => $http_host 'state' => 'failed:invalid_udp_response' 'method' => "$scheme:$method'remote_method' => 'socket' );
            }
            if ( 
    strlen($ret) < $readlength ) {
                return array( 
    'tracker' => $http_host 'state' => 'failed:invalid_udp_packet' 'method' => "$scheme:$method'remote_method' => 'socket' );
            } 
    Last edited by NogDog; 04-21-2014 at 03:32 PM. Reason: added php tags around code

  4. #4
    Join Date
    Jan 2014
    Posts
    104
    A class is a collection of variables and functions working with these variables. Variables are defined by var and functions by function. A class is defined using the following syntax:
    <?php
    class Cart {
    var $items; // Items in our shopping cart

    // Add $num articles of $artnr to the cart

    function add_item($artnr, $num) {
    $this->items[$artnr] += $num;
    }

    // Take $num articles of $artnr out of the cart

    function remove_item($artnr, $num) {
    if ($this->items[$artnr] > $num) {
    $this->items[$artnr] -= $num;
    return true;
    } elseif ($this->items[$artnr] == $num) {
    unset($this->items[$artnr]);
    return true;
    } else {
    return false;
    }
    }
    }
    ?>

  5. #5
    Join Date
    May 2003
    Posts
    599
    Quote Originally Posted by alanps2012 View Post
    I try this

    Code:
    <?php
    
    $meuObjeto = new Torrent();
    
    $meuObjeto->scrape("udp://fopen.demonii.com", 7C90CFEE93DE8C4FA04526DAA5CE530ADFB8DF6E); 
    
    ?>
    not sucess...
    The first problem I see is that the first param of the "scrape()" function is looking for an array and you are passing a string...the second arguement, you are passing a string, but not enclosed in quotes. I have not tested this at all...just throwing this out, but try:

    PHP Code:
    $meuObjeto = new Torrent();
    $meuObjeto->scrape(array("$url1","$url2"), '7C90CFEE93DE8C4FA04526DAA5CE530ADFB8DF6E'); 
    Make sure to enable error reporting and display and some of the errors may become more obvious.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles