private function hit_api()
    {
        if (!$this->_url) {
            Errors::add('transmission extension', 'Config option transmission_api_url not specified');
            return;
        }

        if (!extension_loaded('curl')) {
            Errors::add('transmission extension', 'Curl PHP extension not installed');
            return;
        }

        // See https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt for docs here
        // 1) Hit the API and get back a 409 with a token in the X-Transmission-Session-Id header
        // 2) Pass that token in as a header in the next request which will get our list of torrents

        $curl = curl_init();

        if (!$curl) {
            Errors::add('transmission extension', 'failed initializing curl');
            return;
        }

        $url = $this->_url . '/transmission/rpc';

        curl_setopt_array($curl, [
            CURLOPT_URL => $url,
            CURLOPT_HEADER  => true,
            CURLOPT_RETURNTRANSFER  => true,
        ]);

        $result = curl_exec($curl);

        if (!$result) {
            Errors::add('transmission extension', 'failed running curl to get token');
            return;
        }

        if(!preg_match('/X-Transmission-Session-Id: (\S+)/', $result, $m)) {
            Errors::add('transmission extension', 'Failed parsing token');
            return;
        }

        $token = $m[1];

        $postdata = json_encode([
            'method' => 'torrent-get',
            'arguments' => [
                'fields'=> [
                    'name',
                    'percentDone',
                    'rateDownload',
                    'rateUpload',
                    'leftUntilDone',
                    'eta',
                    'status',
                    'uploadRatio',
                    'downloadedEver',
                    'uploadedEver',
                ]
            ]
        ]);


        curl_setopt_array($curl, [
            CURLOPT_HTTPHEADER => ['X-Transmission-Session-Id: '.$token],
            CURLOPT_URL => $url,
            CURLOPT_POSTFIELDS => $postdata,
            CURLOPT_HEADER  => false,
        ]);

        $result = curl_exec($curl);

        curl_close($curl);

        if (!$result) {
            Errors::add('transmission extension', 'failed running curl to get data');
            return;
        }

        $response = json_decode($result, true);

        if (!$response) {
            Errors::add('transmission extension', 'failed decoding transmission response as json');
            return;
        }

        if ($response['result'] != 'success') {
            Errors::add('transmission extension', 'transmission API call was not successful: '.$response['result']);
            return;
        }

        return $response['arguments']['torrents'];

    }
private function hit_api()
    {
        if (!$this->_url) {
            Errors::add('transmission extension', 'Config option transmission_api_url not specified');
            return;
        }

        if (!extension_loaded('curl')) {
            Errors::add('transmission extension', 'Curl PHP extension not installed');
            return;
        }

        // See https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt for docs here
        // 1) Hit the API and get back a 409 with a token in the X-Transmission-Session-Id header
        // 2) Pass that token in as a header in the next request which will get our list of torrents

        $curl = curl_init();

        if (!$curl) {
            Errors::add('transmission extension', 'failed initializing curl');
            return;
        }

        $url = $this->_url . '/transmission/rpc';

        curl_setopt_array($curl, [
            CURLOPT_URL => $url,
            CURLOPT_HEADER  => true,
            CURLOPT_RETURNTRANSFER  => true,
        ]);

        $result = curl_exec($curl);

        if (!$result) {
            Errors::add('transmission extension', 'failed running curl to get token');
            return;
        }

        if(!preg_match('/X-Transmission-Session-Id: (\S+)/', $result, $m)) {
            Errors::add('transmission extension', 'Failed parsing token');
            return;
        }

        $token = $m[1];

        $postdata = json_encode([
            'method' => 'torrent-get',
            'arguments' => [
                'fields'=> [
                    'name',
                    'percentDone',
                    'rateDownload',
                    'rateUpload',
                    'leftUntilDone',
                    'eta',
                    'status',
                    'uploadRatio',
                    'downloadedEver',
                    'uploadedEver',
                ]
            ]
        ]);


        curl_setopt_array($curl, [
            CURLOPT_HTTPHEADER => ['X-Transmission-Session-Id: '.$token],
            CURLOPT_URL => $url,
            CURLOPT_POSTFIELDS => $postdata,
            CURLOPT_HEADER  => false,
        ]);

        $result = curl_exec($curl);

        curl_close($curl);

        if (!$result) {
            Errors::add('transmission extension', 'failed running curl to get data');
            return;
        }

        $response = json_decode($result, true);

        if (!$response) {
            Errors::add('transmission extension', 'failed decoding transmission response as json');
            return;
        }

        if ($response['result'] != 'success') {
            Errors::add('transmission extension', 'transmission API call was not successful: '.$response['result']);
            return;
        }

        return $response['arguments']['torrents'];

    }
private function hit_api ( ) { if ( ! $ this -> _url ) { Errors :: add ( 'transmission extension' , 'Config option transmission_api_url not specified' ) ; return ; } if ( ! extension_loaded ( 'curl' ) ) { Errors :: add ( 'transmission extension' , 'Curl PHP extension not installed' ) ; return ; } // See https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt for docs here // 1) Hit the API and get back a 409 with a token in the X-Transmission-Session-Id header // 2) Pass that token in as a header in the next request which will get our list of torrents $ curl = curl_init ( ) ; if ( ! $ curl ) { Errors :: add ( 'transmission extension' , 'failed initializing curl' ) ; return ; } $ url = $ this -> _url . '/transmission/rpc' ; curl_setopt_array ( $ curl , [ CURLOPT_URL => $ url , CURLOPT_HEADER => true , CURLOPT_RETURNTRANSFER => true , ] ) ; $ result = curl_exec ( $ curl ) ; if ( ! $ result ) { Errors :: add ( 'transmission extension' , 'failed running curl to get token' ) ; return ; } if ( ! preg_match ( '/X-Transmission-Session-Id: (\S+)/' , $ result , $ m ) ) { Errors :: add ( 'transmission extension' , 'Failed parsing token' ) ; return ; } $ token = $ m [ 1 ] ; $ postdata = json_encode ( [ 'method' => 'torrent-get' , 'arguments' => [ 'fields' => [ 'name' , 'percentDone' , 'rateDownload' , 'rateUpload' , 'leftUntilDone' , 'eta' , 'status' , 'uploadRatio' , 'downloadedEver' , 'uploadedEver' , ] ] ] ) ; curl_setopt_array ( $ curl , [ CURLOPT_HTTPHEADER => [ 'X-Transmission-Session-Id: ' . $ token ] , CURLOPT_URL => $ url , CURLOPT_POSTFIELDS => $ postdata , CURLOPT_HEADER => false , ] ) ; $ result = curl_exec ( $ curl ) ; curl_close ( $ curl ) ; if ( ! $ result ) { Errors :: add ( 'transmission extension' , 'failed running curl to get data' ) ; return ; } $ response = json_decode ( $ result , true ) ; if ( ! $ response ) { Errors :: add ( 'transmission extension' , 'failed decoding transmission response as json' ) ; return ; } if ( $ response [ 'result' ] != 'success' ) { Errors :: add ( 'transmission extension' , 'transmission API call was not successful: ' . $ response [ 'result' ] ) ; return ; } return $ response [ 'arguments' ] [ 'torrents' ] ; }
Use libcurl to interact with the transmission server API
Use libcurl to interact with the transmission server API