Daniel Ruiz
Daniel Ruiz

Reputation: 351

How to send json with curl to a url

I seek in stackoverflow a possible solution but any is good for me. I will send the next json-rpc code to another file. This file contains a bottle server.

curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method":"leer_datos", "params": {"bd":"escuela","tabla":"secretaria"}}' http://localhost:8081/rpc/alchemy

Now, i have in a file the next code for use cURL:

<?php
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://localhost:8081/rpc/alchemy',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => "{'jsonrpc': '2.0','method':'leer_datos','params':{'bd':'escuela','tabla':'secretaria'}}"
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo $resp;
// Close request to clear up some resources
curl_close($curl);
?>

I know the json is not well envoy.

How can i send the json to the url?

Sorry, my english is bad.

Upvotes: 0

Views: 895

Answers (1)

Alexufo
Alexufo

Reputation: 1793

JSON data (key and value) must be only in "double quote".

{
    "jsonrpc": "2.0",
    "method": "leer_datos",
    "params": {
        "bd": "escuela",
        "tabla": "secretaria"
    }
}

Upvotes: 1

Related Questions