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.
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'
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'm creating the authentication and getting an access token fine but the problem arises when I try to send a request. I'm creating the headers using PHP
<?php
define("POST", "POST");
**$environment=' https://api-crt.cert.havail.sabre.com';**
$userId='XXXXXXX';
$domain='AA';
$Group='XXXXXXXXXX';
$formatVersion='V1';
$clientSecret=base64_encode('XXXXXXXXXXX');
$client_id=base64_encode($formatVersion.":".$userId.":".$Group.":".$domain);
$bulid_credential=base64_encode($client_id.":".$clientSecret);
######################## Step 1: Get Token #############################
**$ch =curl_init("https://api-crt.cert.havail.sabre.com/v2/auth/token");**
$vars ="grant_type=client_credentials";
$header =array(
'Authorization: Basic '.$bulid_credential,
'Accept: */*',
'Content-Type: application/x-www-form-urlencoded'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res= curl_exec($ch);
$result=json_decode($res);
$token=$result->access_token;
########################## Step 2: Call the REST API###################
**$url="https://api.test.sabre.com/v3.3.0/shop/flights?mode=live";**
$header = array(
'Authorization: Bearer'. $token,
'Content-Type: application/json'
);
$calltype='POST';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $calltype);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonrequest);//Request
array_push($header, 'Content-Type: application/json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result=curl_exec($ch);
$result1=json_decode($result);
curl_close($ch);
?>
The Sabre Response :
{"status":"Incomplete","type":"Application","errorCode":"ERR.2SG.PROVIDER_ERROR","timeStamp":"2017-10-11T06:02:08.658-05:00","message":"Error occurred while invoking service rest:readMetadata:1.13.7.SNAPSHOT"}
1) Did you ensure that $token is a string and not an object?
I use this:
$result=json_decode($res, TRUE);
$token=$result['access_token'];
Instead of this:
$result=json_decode($res);
$token=$result->access_token;
2) Did you ensure that the content of $jsonrequest is valid?
Maybe it's not the PHP code that is incorrect, but your JSON query.
/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.