Json API with PHP to get torrents info

General support for problems installing or using Deluge
raspdealer
Leecher
Leecher
Posts: 50
Joined: Sun Oct 04, 2015 6:11 pm
OS or Distro: Raspbian

Json API with PHP to get torrents info

Postby raspdealer » Sun Jun 11, 2017 9:22 pm

Hi there,

Please find below a python script I've made to get my list of torrents:

Code: Select all

#!/bin/bash
sudo 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://192.168.1.106:8112/json
json=$(sudo curl -b cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method": "web.update_ui", "params": [["tracker", "label"], {}], "id": 1}' http://192.168.1.106:8112/json)
sudo rm cookies.txt
echo $json


I would like to achieve the same result with PHP but it doesn't work.
Can you please help?

Here is the code:

Code: Select all

<?php
   // Server URL
   $url = "http://192.168.1.104:8112/json";
   // Define the cookie file location
   $cookiefile = "/var/www/html/cki-deluge.txt";
   // Auth
   $login = curl_init();
   curl_setopt( $login, CURLOPT_URL, $url);
   curl_setopt( $login, CURLOPT_POST, 1);
   curl_setopt( $login, CURLOPT_POSTFIELDS, '{"method": "auth.login", "params": ["password"], "id": 1}');
   curl_setopt( $login, CURLOPT_COOKIEJAR, $cookiefile);
   curl_setopt( $login, CURLOPT_COOKIEFILE, $cookiefile);
   curl_setopt( $login, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt( $login, CURLOPT_SSL_VERIFYHOST, false);
   curl_setopt( $login, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt( $login, CURLOPT_HEADER, 1);
   curl_setopt( $login, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt( $login, CURLOPT_ENCODING, "gzip");
   curl_exec($login);
   curl_close($login);
   echo 'COOKIE: ' . file_get_contents($cookiefile);
   
   //Get info
   $query = curl_init();
   curl_setopt( $query, CURLOPT_URL, $url);
   curl_setopt( $query, CURLOPT_POSTFIELDS, '{"method": "web.update_ui", "params": [["tracker", "label"], {}], "id": 1}');
   curl_setopt( $query, CURLOPT_COOKIEJAR, $cookiefile);
   curl_setopt( $query, CURLOPT_COOKIEFILE, $cookiefile);
   curl_setopt( $query, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt( $query, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt( $query, CURLOPT_SSL_VERIFYHOST, false);
   curl_setopt( $query, CURLOPT_ENCODING, "gzip");
   $parseResponse = curl_exec($query);
   curl_close($query);
   echo $parseResponse;
?>


THANKS!

Cas
Top Bloke
Top Bloke
Posts: 3463
Joined: Mon Dec 07, 2009 6:04 am
OS or Distro: Ubuntu 16.04
Location: Scotland

Re: Use Json API to get torrents info

Postby Cas » Mon Jun 12, 2017 7:04 am

You haven't said what error you are seeing but I do notice you have not set the content-type application/json. Another issue might be to do with cross-site scripting which webui doesn't support.

raspdealer
Leecher
Leecher
Posts: 50
Joined: Sun Oct 04, 2015 6:11 pm
OS or Distro: Raspbian

Re: Use Json API to get torrents info

Postby raspdealer » Mon Jun 12, 2017 6:08 pm

I'm dumb.
Adding curl_setopt( $login, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); fixed it.

Thanks!

raspdealer
Leecher
Leecher
Posts: 50
Joined: Sun Oct 04, 2015 6:11 pm
OS or Distro: Raspbian

Re: Json API with PHP to get torrents info

Postby raspdealer » Mon Sep 25, 2017 5:28 pm

Hi,

I'm just coming back to this topic otherwise I will be hairless soon.
The bash script I have provided stopped working and I don't know why.

Do you have any ideas?
The cookie is ok but after I got this:
{"id": 1, "result": true, "error": null}
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed
100 194 0 120 100 74 46 28 0:00:02 0:00:02 --:--:-- 46
{"id": 1, "result": {"stats": {"max_upload": null, "max_download": null, "max_num_connections": null}, "connected": false, "torrents": null, "filters": null}, "error": null}

I'm getting crazy :S


Return to “Support”

Who is online

Users browsing this forum: No registered users and 9 guests