I have some data I need to post to an endpoint which is a nested array like:
$contentPostData = array(
'contents' => array(
'name' => $comment_text,
'fingerprint' => $fingerprint,
'signers' => array(
$ownerAuthId
)
)
);
When I try:
json_encode($contentPostData);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
I get the error:
string(518) "{"timestamp":1578447151168,"message":"Can't read request","details":["JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException
How do I format the contentPostData so that it can be deserialized correctly?
You mention:
json_encode($contentPostData);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
But are you assigning the encoded json to $data? I mean:
are you doing:
$data = json_encode($contentPostData);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
I see that the webservice to which you are calling uses com.fasterxml.jackson so most likely expects JSON format and therefore your serialization should be okay (if field names and types match).
I also suggest setting the header to inform that webservice that you are sending a json file (in case it allows other formats):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
);
So:
$data = json_encode($contentPostData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
If that does not work then most likely: a) your JSON is invalid (different structure is expected) or b) the webservice actually expects a different format than JSON (maybe standard multipart data for example)
Related
I am having the following code to make a GET statement to the REST API of Parse server using PHP:
$query = json_encode(
array(
'where' => array( 'userid' => "8728792347239" )
));
echo $query;
$ch = curl_init('https://*hidden*.herokuapp.com/parse/classes/computers?'.$query);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'X-Parse-Application-Id: *hidden*',
'X-Parse-REST-API-Key: *hidden*',
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
print_r($response);
However I am getting the following error:
{"code":102,"error":"Invalid parameter for query: {\"where\":{\"userid\":\"8728792347239\"}}"}
What am I doing wrong?
without having read the documentation, i bet it's supposed to be url-encoded, not json-encoded -OR- that the data is supposed to be in the POST body, not the URL query.
if guess #1 is correct, then your problem is that you're using json_encode instead of http_build_query eg
$query = http_build_query(
array(
'where' => array( 'userid' => "8728792347239" )
));
if guess #2 is correct, then your problem is that you're adding the data to the url query instead of adding it to the request body, eg
$ch = curl_init('https://*hidden*.herokuapp.com/parse/classes/computers');
curl_setopt($ch,CURLOPT_POSTFIELDS,$query);
After executing the PHP Script, I am getting the following error – Any help is appreciated. Based on my understanding, I created an array based on the bugzilla documentation - Thanks in advance
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/bug.html#create-bug
{"code":32000,"error":true,"message":"Could not parse the 'params' argument as valid JSON. Error: malformed number (no digits after initial minus), at character offset 1
$url ="http://localhost:8080/bugzilla/rest/bug";
$data = array(
"product" => "TestProduct",
"component" => "TestComponent",
"version" => "unspecified",
"summary" => "'This is a test bug - please disregard",
"alias" => "SomeAlias",
"op_sys" => "All",
"priority" => "P1",
"rep_platform" => "All"
);
$str_data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: application/json", "Accept: application/json"));
$username = 'ashish.sureka#in.abb.com';
$password = 'incrc';
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
$result = curl_exec($ch);
curl_close($ch);
echo $result
You have this:
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
which sends your array of data as a conventional key=value form submission, when you should have
curl_setopt($ch, CURLOPT_POSTFIELDS, $str_data);
^^^^^
which would send your JSON-encoded array instead.
You send $data in CURLOPT_POSTFIELDS but I think you need to give it $str_data which is encode in JSON
I think you have to replace
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
with
curl_setopt($ch, CURLOPT_POSTFIELDS, $str_data);
to transfer the json encoded array instead of the raw php array.
I am trying to send file via curl, but keep getting this error:
PHP Notice: Array to string conversion in /home/sasha/Documents/OffProjects/test/index.php on line 26
This is my code at the moment:
$target_url = 'https://address';
$file_name_with_full_path = realpath('mpthreetest.mp3');
$post = [
'textNote' => 'This is test',
'audioFile' => '#'.$file_name_with_full_path,
'department' => 'Test',
'timeDetected' => round(microtime(true) * 1000),
'subjectLine' => 'Test Test',
'recipients' => ['phone-number' => '111111111']
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
How can I make this working?
You can't have an array as a value, it's trying to convert the post data into a string. Check what the website you are posting to is expecting for "recipients", my guess would be some form of JSON string.
When you pass an array to CURLOPT_POSTFIELDS it will try to build a form-data content (see http://php.net/manual/en/function.curl-setopt.php for details).
But you have nested arrays here:
....
'recipients' => ['phone-number' => '111111111']
...
and this cannot work. You'll have to find a different way to pass multiple values as recipients, either as a JSON string as #fire mentioned, or with a serialized value, or whatever transformation you prefer.
I have created a php file that sends json data to an external url. Their API requires that I have a page on my website that receives the json responses for processing. The one that sends json data works well. I need help with the second php page that should process it
$Url="http://example.com/submit.php";
$date = date_create();
$UserID=7;
$Password='';//<-password written here
$Timestamp=date_timestamp_get($date);
$token=$UserID.$Password.$Timestamp;
$data_string = array();
$data_string = array(
"AuthDetails" => array(
array(
"UserID" => $UserID,
"Token" => md5($token),
"Timestamp"=>$Timestamp
)
),
"MessageType"=> array(
"3"
),
"BatchType"=>array(
"0"
),
"SourceAddr"=>array(
"Example"
),
"MessagePayload"=> array(
array(
"Text" => "Sample text message by Example :)"
)
),
"DestinationAddr" => array(
array(
'MSISDN'=>'254701000000',
'LinkID'=>''
)
),
"DeliveryRequest" => array(
array(
'EndPoint'=>'',//<-URL that receives the response
'Correlator'=>md5(uniqid())
)
)
);
$data_string=json_encode($data_string);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
)
);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$output = curl_exec($ch);
if(curl_errno($ch)){
echo 'Request Error:' . curl_error($ch);
}
curl_close($ch);
return $output;
Ok this is going to be a bit generic but it should put you on the right path.
So you have to write a script that will process the response from them i.e. your end-point for this circle of events.
So lets call it my-enpoint.php
So in the message you send to them you put the address of this new script into this parameter
"DeliveryRequest" => array(
array(
'EndPoint'=>'http://www.example.com/my-endpoint.php',
'Correlator'=>md5(uniqid())
Now your script my-endpoint.php I am assuming they have said how they will return their reply, probably as POST variable. You process their reply basically like you would a submitted form from your own site.
So for example if their reply is POSTed
<?php
// initial testing to see what comes back from them
// as this wont be associated with a browser
// dump their reply to a file so you can see whats there
file_put_contents('reply.txt', print_r($_POST, true), FILE_APPEND);
?>
With the info you have provided this is about a much as I can do.
I am trying to send POST data via cURL however it is not sending the data, however when I send them as GET variables it is sending, does anyone know what the problem could be?
curl_setopt_array($ch, array(
CURLOPT_URL => "local.new.api.test.com/authenticate/"
));
$data = array(
'username' => 'test',
'password' => 'test'
);
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
CURLOPT_POSTFIELDS is not a json encoded string. It requires a query string (similar to the query string in the URL-s after the question mark).
This should work:
$data_string = http_build_query($data, '', '&');
Or you can let the cURL extension to do the dirty work and pass an array with key-value pairs as the value:
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
From PHP.net
This parameter can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.