/Unable to Send Multidimensional Json array through php cURL .
so in the below code i send it as array of Objects which will be difficult to retrieve in python/
$_api_url="http://example.com" ;
$params = http_build_query(array('data_Details' => json_encode($request)));
//initialize and setup the curl handler
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
//execute request
$result = curl_exec($ch);
//close connection
curl_close($ch);
Try looking at the following example: POSTing JSON Data With PHP cURL
Useful excerpt:
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$data_string = json_encode($request);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$request should be associated array format.
Related
I am trying to consume a service that I exposed from a project that I have in c #, the problem is that when consuming the service from php I get the following error.
{"error":"unsupported_grant_type"}
Code:
function __construct() {
# data needs to be POSTed to the Play url as JSON.
# (some code from http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl)
$data = array("grant_type" => "password", "username" => "Administrador", "password" => "xxxx");
$data_string = json_encode($data);
print_r($data_string);
$ch = curl_init('https://integraciones.intergrupo.com/rest/oauth/token');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded')
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result; // Outputs the JSON decoded data
}
You are sending a header 'Content-Type: application/x-www-form-urlencoded'.
With that you shouldn't json_encode $data, but just add the array to POSTFIELDS:
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
OR change the header to 'Content-Type: application/json'
CURLOPT_POSTFIELDS does not accept json_encode, but if I directly write the json in the CURLOPT_POSTFIELDS if it is entered
This returns an error
{"error":"bad_request","reason":"Request body must be a JSON object"}
$bd = "fiscont_db_catalogo_cuentas";
$ch = curl_init();
$document ='{"docs":[{"key":"baz","name":"bazzel"},{"key":"bar","name":"barry"}]}';
$json = json_encode($document);
echo $json;
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5984/'.$bd.'/_bulk_docs');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); /* or PUT */
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Accept: */*'
));
curl_setopt($ch, CURLOPT_USERPWD, 'root:addc1243c');
$response = curl_exec($ch);
echo $response;
curl_close($ch);
but this is returning :
[{"ok":true,"id":"8b3c672ffd4b8dcd7da313e9e9011243","rev":"1-f5f3f3e496c72307975a69c73fd53d42"},{"ok":true,"id":"8b3c672ffd4b8dcd7da313e9e9011c5a","rev":"1-8ad0e70d5e6edd474ec190eac2376bde"}]
$bd = "fiscont_db_catalogo_cuentas";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5984/'.$bd.'/_bulk_docs');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); /* or PUT */
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"docs\":[{\"key\":\"baz\",\"name\":\"bazzel\"},{\"key\":\"bar\",\"name\":\"barry\"}]}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Accept: */*'
));
curl_setopt($ch, CURLOPT_USERPWD, 'root:addc1243c');
$response = curl_exec($ch);
echo $response;
curl_close($ch);
json_encode takes an array or an object as a parameter. In your example, you encode a JSON string to JSON which doesn't make sens.
If you replace your line by this:
$document =['docs' => [['key'=>'baz','name'=>'bazzel'],['key'=>'bar','name'=>'barry']];
It will correctly convert your PHP associative array to JSON.
I am creating a script that passes on some json formatted data. I'm having a issue encoding it correctly. Below is my code:
$data = array("email" => "theemail#email.com");
$data_string = json_encode(array('profiles' => $data));
$ch = curl_init('https://theurlisHere');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
I need my json to look like this:
"profile": [
{
"email": "theemail#email.com"
}
]
I can't figure out how to get the 'profile' object in to the array.
Thanks in advance.
You can use
json_encode(array('profile' => array($data)));
follow codes is php code ,but it don't download anything.
$postdata=$jsonstr;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
//'Content-Type: application/x-www-form-urlencoded"',
'Content-Length: ' . strlen($postdata)
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata );
$output = curl_exec($ch);
$info = curl_getinfo($ch);
echo "<br/>-------------------<br/>";
//print_r($output);
echo "<br/>-------------------<br/>";
mySaveFile($output, "./file.tmp");
curl_close($ch);
I get 'file.tmp' always is size 0.I suspect I set up a mistake for CURLOPT_HTTPHEADER,
thanks for everyone help.
I am trying to add a track to my own playlist with a little php script, but it won't work.
I always get the errror:
{ "error" : { "status" : 400, "message" : "Error parsing JSON." } }
This is the spotify document for adding tracks:
https://developer.spotify.com/web-api/add-tracks-to-playlist/
Has anybody an idea what the problem could be?
$token='my Access token';
$headers = array(
"Accept: application/json",
"Authorization: Bearer " . $token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/users/*myusername*/playlists/*myplaylistID*/tracks' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, 'uris=spotify:track:3DXncPQOG4VBw3QHh3S817' );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec($ch);
print "$result";
$result = json_decode($result, true);
curl_close($ch);
I solved it finally:
$key='***AccessKey***';
$url = 'https://api.spotify.com/v1/users/USERID/playlists/playlistID/tracks?uris=spotify%3Atrack%3A3DXncPQOG4VBw3QHh3S817';
$headers = array(
"Content-Length: 0",
"Accept-Encoding: gzip, deflate, compress",
"User-Agent: runscope/0.1",
"Content-Type: application/json",
"Authorization: Bearer ".$key);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($response);
Try this:
$data = array('uris' => 'spotify:track:3DXncPQOG4VBw3QHh3S817');
$data_json = json_encode($data);
// ...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/...');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array
(
'Content-Type: application/json',
'Content-Length: '.strlen($data_json),
'Authorization: Bearer '.$token
));
$result = curl_exec($ch);
// ...