Problem in PHP curl and the Transmission RPC

Discussion of the Web Interface for Transmission, formerly known as Clutch. This applies to all version of Transmission
Post Reply
aibo99
Posts: 6
Joined: Tue Jan 06, 2009 4:30 am

Problem in PHP curl and the Transmission RPC

Post by aibo99 »

Hello,

I am trying to use PHP curl to send request to the Transmission RPC, but it only returns an error message to me.

My PHP code:

<?php
$a = array('method'=>'session-stats');

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://192.168.1.220:9091/transmission/rpc");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $a);

$r = curl_exec($ch);
curl_close($ch);
?>

The error message that Transmission returns:

{
"arguments": {},
"result": "no method name"
}

What's wrong ?

Appreciate it if you could offer me some help.

Thanks
Gimp
Web Interface Developer
Posts: 257
Joined: Mon Mar 12, 2007 3:49 am
Location: BC, Canada

Re: Problem in PHP curl and the Transmission RPC

Post by Gimp »

If you're using HTTP_POST, you have to send json data. in order to send a simple method=something, you must use GET, not POST.
Jordan
Transmission Developer
Posts: 2312
Joined: Sat May 26, 2007 3:39 pm
Location: Titania's Room

Re: Problem in PHP curl and the Transmission RPC

Post by Jordan »

Although, I will say that POST is definitely the preferred method.

The GET syntax is basically a convenience hack and isn't officially supported.
aibo99
Posts: 6
Joined: Tue Jan 06, 2009 4:30 am

Re: Problem in PHP curl and the Transmission RPC

Post by aibo99 »

Thank you for your reply.

How to "post" a json object using PHP curl ?
Jordan
Transmission Developer
Posts: 2312
Joined: Sat May 26, 2007 3:39 pm
Location: Titania's Room

Re: Problem in PHP curl and the Transmission RPC

Post by Jordan »

aibo99
Posts: 6
Joined: Tue Jan 06, 2009 4:30 am

Re: Problem in PHP curl and the Transmission RPC

Post by aibo99 »

I solved the problem. Thank you for your help
mmaura
Posts: 2
Joined: Sat Sep 12, 2009 9:07 am

Re: Problem in PHP curl and the Transmission RPC

Post by mmaura »

i have make this, it s work:

Code: Select all

#!/usr/bin/php

<?php
$HOST="127.0.0.1" ;
$PORT="9091" ;
$USER="me" ;
$PASSWORD="mypassword" ;

$json = array("arguments" => "{}",
"method" => "session-stats") ;

$a = json_encode($json) ;

$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_URL, "http://$HOST:$PORT/transmission/rpc");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $a);
curl_setopt($ch, CURLOPT_HTTPAUTH, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$USER:$PASSWORD"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$r = curl_exec($ch);

//$ret = preg_match  ( "%.*<code>(X-Transmission-Session-Id: .*?)(</code>.*)%", $r, $result) ;
$ret = preg_match  ( "%.*\r\n(X-Transmission-Session-Id: .*?)(\r\n.*)%", $r, $result) ;
$X_Transmission_Session_Id  = $result[1] ;


curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array ($X_Transmission_Session_Id)) ;
$r = curl_exec($ch);

curl_close($ch);

/***********************************************************************/
$stats = json_decode($r, true) ;

$stats = $stats ["arguments"] ;

//print_r ($stats) ;


?>
deadeyes
Posts: 20
Joined: Wed Jun 16, 2010 7:02 am

Re: Problem in PHP curl and the Transmission RPC

Post by deadeyes »

Nice piece of code.

Just tried something: https://trac.transmissionbt.com/browser ... c-spec.txt
On line 297 there is some example call.
However it is not written in any specific programming language.
I tried it in php:

Code: Select all

         $rpccall = array( "arguments" => '"fields": [ "id", "name", "totalSize" ], "ids" : [ 1 ]', "method" => "torrent-get" ) ;
But this does not seems right.
How should I do this?

Thanks in advance

Edit:
I found it :)
In php there are no different structures for "arrays" with keys and "arrays" without keys.
So what is between { } represents an array which uses a key (the part before ":").
In php:

Code: Select all

$var = array ("key"=>"value");
In json

Code: Select all

{"key":"value"}
What is in between [] in json is an "array" without a key. However in php there is always a key.
When the key is not given in php it gets a number.

Code: Select all

$var = array ("value");
In json

Code: Select all

[1,2,3]
This accomplishes the example from the docs:

Code: Select all

       $fields = array ( "id", "name", "totalSize" );
        $ids = array ( 1 );
        $arguments = array ( "fields" => $fields, "ids" => $ids );
        $rpccall = array( "arguments" => $arguments, "method" => "torrent-get" ) ;
        $rpccallencoded = json_encode ($rpccall);
So just nest those php arrays and use json_encode and json_decode functions to convert from one to another.

Furthermore if you are not sure you can print the json string and compare that with the output transmission-remote --debug ...

I hope this helps for anybody else
Post Reply