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
Problem in PHP curl and the Transmission RPC
Re: Problem in PHP curl and the Transmission RPC
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.
Re: Problem in PHP curl and the Transmission RPC
Although, I will say that POST is definitely the preferred method.
The GET syntax is basically a convenience hack and isn't officially supported.
The GET syntax is basically a convenience hack and isn't officially supported.
Re: Problem in PHP curl and the Transmission RPC
Thank you for your reply.
How to "post" a json object using PHP curl ?
How to "post" a json object using PHP curl ?
Re: Problem in PHP curl and the Transmission RPC
I solved the problem. Thank you for your help
Re: Problem in PHP curl and the Transmission RPC
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) ;
?>
Re: Problem in PHP curl and the Transmission RPC
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:
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:
In json
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.
In json
This accomplishes the example from the docs:
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
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" ) ;
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");
Code: Select all
{"key":"value"}
When the key is not given in php it gets a number.
Code: Select all
$var = array ("value");
Code: Select all
[1,2,3]
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);
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