Page 1 of 1

Get a list of torrent statuses via json rpc using php and curl.

Posted: Thu Jan 19, 2017 11:09 am
by zarexogre
I have the following code which seems to authorized successfully as I get session back, but then i try and call the get_torrents_status and I get 500 error, I have run daemon with -L option but the log is no help. Can you help advise on how to get a list of torrents statuses in php, have searched everywhere and no joy.

Code: Select all


  public function deluge_auth($deluge_url, $deluge_password) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $deluge_url);
    curl_setopt($curl, CURLOPT_POSTFIELDS, '{"method": "auth.login", "params": ["'.$deluge_password.'"], "id": 1}');
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_ENCODING, "gzip");
    $data = curl_exec($curl);
    curl_close($curl);
    preg_match_all('|Set-Cookie: (.*);|U', $data, $matches);
    $cookies = implode('; ', $matches[1]);
    return $cookies;
  }

  public function data_widget_torrents($node) {
    $deluge_url = $node->field_deluge_url->value . 'json';
    $deluge_password = $node->field_deluge_password->value;
    $rpc_call = 'core.get_torrents_status';
    $cookies = $this->deluge_auth($deluge_url, $deluge_password);
   
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $deluge_url);
    curl_setopt($curl, CURLOPT_POSTFIELDS, '{"method":"' . $rpc_call . '"}');
    curl_setopt($curl, CURLOPT_COOKIE, $cookies);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_ENCODING, "gzip");
    $data = curl_exec($curl);
    curl_close($curl);

    p($data);
  }
This is the debug info I output to the browser:

Code: Select all


http://myserver.com:8112/json

core.get_torrents_status

_session_id=51b2a09f63d847be1705d1cc88c9c4762201

HTTP/1.1 500 Internal Server Error
Date: Thu, 19 Jan 2017 11:05:26 GMT
Content-Length: 0
Content-Type: text/html
Server: TwistedWeb/14.0.2


Re: Get a list of torrent statuses via json rpc using php and curl.

Posted: Thu Jan 19, 2017 3:51 pm
by Cas
Have you tested with just curl from commandline?

Re: Get a list of torrent statuses via json rpc using php and curl.

Posted: Mon Jan 23, 2017 9:43 am
by zarexogre
Not 100% how I would do this with auth? Any chance you have an example of this using curl from command line?

Re: Get a list of torrent statuses via json rpc using php and curl.

Posted: Mon Jan 23, 2017 9:48 am
by zarexogre
Found a post that showed me how to do the auth with curl command line:

Code: Select all

curl -c cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method": "auth.login", "params": ["password"], "id": 1}' http://myhost.com:8112/json

curl -b cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method": "core.get_torrents_status"}' http://myhost.com:8112/json
Same result:

HTTP/1.1 500 Internal Server Error
Date: Mon, 23 Jan 2017 09:51:50 GMT
Content-Length: 0
Content-Type: text/html
Server: TwistedWeb/14.0.2

Kinda hoped I had the function name wrong, please any help would be great otherwise I might have to use something other than deluge which would make me sad!

Re: Get a list of torrent statuses via json rpc using php and curl.

Posted: Mon Jan 23, 2017 10:46 am
by Cas
Are you testing remotely or locally to deluge-web? Could it be you are running into a Cross Origin Resource Sharing (CORS) issue?

Re: Get a list of torrent statuses via json rpc using php and curl.

Posted: Wed Jan 25, 2017 10:02 am
by zarexogre
Could be I am testing remotely, I will dump code on server and try and see what happens.

Re: Get a list of torrent statuses via json rpc using php and curl.

Posted: Wed Jan 25, 2017 10:20 am
by zarexogre
Same result:

http://myserver.com:8112/json

core.get_torrents_status

_session_id=a1ca19a23d141a1d352132270435511e2007

HTTP/1.1 500 Internal Server Error
Date: Wed, 25 Jan 2017 10:19:44 GMT
Content-Length: 0
Content-Type: text/html
Server: TwistedWeb/14.0.2

Re: Get a list of torrent statuses via json rpc using php and curl.

Posted: Wed Jan 25, 2017 12:12 pm
by Cas
This isn't an issue with json-rpc this is a problem with access to deluge-web url. Unless you are still trying to use php in which case test with curl from command line locally. I have no answers so you will need to problem solve this yourself. e.g.

Can you access deluge-web from 127.0.0.1:8112?
Are you running apache or nginx?
Are you testing with 127.0.0.1:8112/json?
What do you get if you point your browser to that page?

Re: Get a list of torrent statuses via json rpc using php and curl.

Posted: Wed Jan 25, 2017 5:17 pm
by Cas
I just realised what is being missed here. First of all you need to enable logging on deluge-web to see any critical errors to do with json-api. Then you would see this is an invalid json request and core.get_torrents_status requires two parameters:

Code: Select all

curl -b cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method": "core.get_torrents_status"}' http://myhost.com:8112/json

[ERROR   ] 17:13:48 json_api:279 Invalid JSON request
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/deluge/ui/web/json_api.py", line 301, in render
    d = self._on_json_request(request)
  File "/usr/lib/python2.7/dist-packages/deluge/ui/web/json_api.py", line 265, in _on_json_request
    response["id"], d, response["error"] = self._handle_request(request)
  File "/usr/lib/python2.7/dist-packages/deluge/ui/web/json_api.py", line 220, in _handle_request
    raise JSONException("Invalid JSON request")
JSONException: Invalid JSON request
HTTP/1.1 500 Internal Server Error

Content-Length: 0
Content-Type: text/html
Server: TwistedWeb/16.0.0
So it should be:

Code: Select all

curl -b cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method": "core.get_torrents_status", "params": [[],[]], "id": 1}' http://127.0.0.1:8112/json
Starting with the basics and using the examples exactly as they were pasted would have avoided this. However when I have time I shall put together a wiki page for the json-api with examples and pitfalls.