Jump to content

PHP CURL / JSON-RPC Problem


blunt

Recommended Posts

Hello, I am having a problem using CURL to query a JSON-RPC server, If I do the following using CURL via commandline:
[quote]
-bash-2.05b$ curl --data-binary '{"method": "push", "value": "hello"}' -H 'content-type: text/plain;'  http://192.168.5.37:8080/json
"OK"-bash-2.05b$
-bash-2.05b$ curl --data-binary '{"method": "pop"}' -H 'content-type: text/plain;'  http://192.168.5.37:8080/json
"hello"-bash-2.05b$
[/quote]
This executes successfully as you can see, now I have the following PHP Code:
[code]
<?php
// setup json data
$url = "http://192.168.5.37:8080/json";
$post_data = array('method' => 'push', 'value' => 'testing');
$post_data = json_encode($post_data);
echo $post_data.'<br/>';
// execute json request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 'content-type: text/plain;');
curl_setopt($ch, CURLOPT_TRANSFERTEXT, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// decode result
if ($result = @curl_exec($ch)) {
echo 'query success<br/><pre>';
print_r(json_decode($result));
echo '</pre>';
} else {
'query failed';
}
curl_close($ch);
?>
[/code]
Script outputs the following:
[quote]
{"method":"push","value":"testing"}
query success

stdClass Object
(
    [response] => stdClass Object
        (
            [error] => Unkown method, list of known methods: ['push', 'pop']
        )

)
[/quote]

Is there anything here I am doing wrong, I have set it to post the data using plaintext in the correct json format... something I'm missing here?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.