简体   繁体   中英

XBMC api get request in php with curl

I'm trying to do a GET request to the XBMC api in PHP, but i cant get it to work. I already have a valid url, thats working when i simulate a GET request with a REST client.

http://localhost:8085/jsonrpc?request={"jsonrpc": "2.0", "id": 1, "method": "Input.ExecuteAction", "params": {"action":"left"}}

I get a parse error from xbmc when i simulate a POST:

{
"error": {
    "code": -32700,
    "message": "Parse error."
},
    "id": null,
    "jsonrpc": "2.0"
}

This is the code i currently have:

<?php
    $jsonString = "{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"Input.ExecuteAction\", \"params\": {\"action\":\"left\"}}";
    $url = "http://localhost:8085/jsonrpc?request=".$jsonString;

    get($url);

    function get($url){
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7);

        $result = curl_exec($ch);
        print $result;
        curl_close($ch);
   }

But this wont do the job, just like when i simulate a POST request a parse error is returned. So my thoughts are that i'm doing a POST request, am i? If no, what would be the problem then?

I just ran into the same problem. You need to encode the url you are using for XBMC (Kodi nowadays). So in your case:

$jsonString

should be replaced by

urlencode($jsonString)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM