I have an api which works fine when I use POSTMAN, but now that I am trying to send it with PHP I am getting an API error message in response.
Format Error: The exception message is 'The incoming message has an unexpected message format 'Raw'
Which I am sure is related to my data structure. can someone tell me where I am going wrong?
Header
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
JSON Body
{
"accessID": "ASASD22",
"password": "DASD2DQA",
"messages": [
{
"DestinationID": "22D2D2D22D",
"UserMessageID": 133,
"RawPayload": [1,31, 34, 43]
}
]
}
Here is the PHP code I wrote, but returns an error code; can you guys see any issues?
$headers= array('Connection: Keep-Alive','Content-Type: application/json; charset=utf-8','Connection: Keep-Alive');
$data = array(
"accessID" => "ASASD22",
"password" => "DASD2DQA",
"messages" => array(
"DestinationID" => "22D2D2D22D",
"UserMessageID" => "133",
"RawPayload" => "[1,31, 34, 43]"
)
);
$url_send ="http://api.SITE.com/RST-MESSAGE.svc/submit.json/";
$str_data = json_encode($data);
function sendPostData($url, $post){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo " " . sendPostData($url_send, $str_data);
It's difficult to guess, you should check the API's documentation.
But, if the data posted as "JSON Body" is the request you send when it works then you should replicate it exactly in PHP (which you don't):
$data = array(
"accessID" => "ASASD22",
"password" => "DASD2DQA",
"messages" => array(
array(
"DestinationID" => "22D2D2D22D",
"UserMessageID" => "133",
"RawPayload" => array(1, 31, 34, 43),
),
)
);
I did two changes to your data structure. See below the pieces of JSON body that ask for changes in the structure of $data:
"messages": [ { ... } ] - messages is an array of objects, not a single object;
"RawPayload": [1, 31, 34, 43] - RawPayload is an array of integers, not a string;
I found a way to correctly send the entire JSON with in the PHP cURL function
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.SITE.com/Messages.svc/submit.json/" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, '{
"accessID": "ASASD22",
"password": "DASD2DQA",
"messages": [
{
"DestinationID": "22D2D2D22D",
"UserMessageID": 133,
"RawPayload": [1,31, 34, 43]
}
]
}' );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json'));
$result=curl_exec ($ch);
echo $result;
?>
so basically I wrapped the entire JSON into curl_setopt($ch, CURLOPT_POSTFIELDS, $data) and send it that way!
Related
How to send values (Dynamic variables) to external JSON API (Post Method) in Drupal 7?
$data = array("name" => $name, "desc" => $desc,"email" => $email,"lastname" => $lastname);
$data_string = json_encode($data);
$ch = curl_init('https://ur);
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);
From my controller, I'm trying to make a post request to https://test.adyen.com/hpp/select.shtml but I continue receiving this error
Error: Skin null does not exist
This is part of my code
$paramsRequest = array (
'merchantReference' => 'reference',
'paymentAmount' => 10,
'currencyCode' => 'EUR',
'shipBeforeDate' => '2017-11-29T13:42:40+1:00',
'skinCode' => 'f8My4RJC',
'merchantAccount' => 'TestNL076',
'sessionValidity' => '2017-11-29T13:42:40+1:00',
'merchantReturnData' => 'string',
'shopperEmail' => 'shopper#gmail.com',
);
$hcma = '8382E2...';
$paramsRequest['merchantSig'] = Util::calculateSha256Signature($hcma, $paramsRequest);
return $this->handlePostRequest($paramsRequest, 'https://test.adyen.com/hpp/select.shtml');
and to handle the request
protected function handlePostRequest($data, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERPWD, 'admin' . ":" . 'password');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $result;
}
Are you trying to perform a lookup of available payments? If so, use the end point.
https://test.adyen.com/hpp/directory.shtml
Otherwise the select.shtml an endpoint to redirect shopper's browser when you want them to enter in information or send them to their bank. Here's link to adyen's local payment method docs.
Is there actually a required json object which each push service requires. For example I always see the following in every tutorial:
Android:
var notification = "{\"data\":{\"msg\":\"Breaking " + category + " News!\"}}";
Apple:
var alert = "{\"aps\":{\"alert\":\"Breaking " + category + " News!\"}}";
Do these structures have to be kept? or can I send my own custom objects down to the phone?
I think your question is quite specific to certain platform you are trying to send the Push Notification. If you want to just implement the GCM on standard Android Native App.. You can just embed the code in your server implementation.
public function send_notification($registatoin_ids, $message) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
So the answer to your question is No.
function post($url, $params, $headers) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$parent_id = 0;
$name = 'examplefolder';
$params = array('name' => $name => json_encode(array('id' => $parent_id)));
$resp = post('https://api.box.com/2.0/folders', $params, array('Authorization: Bearer ' . $ACCESS_TOKEN_HERE));
When this code executes I got this error in Json format
{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"missing_parameter","name":"parent","message":"'parent' is required"},{"reason":"missing_parameter","name":"name","message":"'name' is required"}]},"help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Bad Request","request_id":"204107642652b93aceca050"}
Is there anything I'm missing?
Additional note.
I even tried to make the params a query string like this.
name=examplefolder&parent={"id":0}
and this gives me a different error
{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"entity-body","message":"Invalid value 'name=examplefolder&parent={\"id\":0}'. Entity body should be a correctly nested resource attribute name\/value pair"}]},"help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Bad Request","request_id":"154368831052b93bb027779"}
I'm not totally familiar with this language syntax, but it looks to me like the parent object is not declared in your POST body. You need to be sending this:
{"name":NAME, "parent":{"id":"0"}}
You might be able to accomplish that by changing your $params like so:
$params = array('name' => $name, 'parent' => json_encode(array('id' => $parent_id)));
Also, per the documentation the $parent_id should be serialized as a string, not as an integer.
{"name":NAME, "parent":{"parent_id":"0"}}
This worked for me when I was working Angular 1 - angular file upload module.
However it is different with angular2, still working on..
vm.uploader.onBeforeUploadItem = function (item) {
if (!vm.parentFolderId || item.isVersionUpload) return;
item.headers = { Authorization: 'Bearer ' + vm.userToken };
item.formData = {
name: item.file.name,
parent: {
parent_id: vm.parentFolderId
}
};
}
i want to post json data with php curl for a multipart/form-data with a file upload field.
i tried this in cakephp 2 in a action:
public function json_post(){
$this->autoRender = false;
debug('json post test');
$data = array('Post' => array(
'subject' => 'test subject content',
'body' => 'test body content'
'fileName' => '/Users/mar/Pictures/cow_wall_90.jpg'
));
$data_string = json_encode($data);
$ch = curl_init('http://www.mydomain.com/posts/phone_upload.json');
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: multipart/form-data',
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
debug($result);
}
fileName is the equivalent for the file upload field in a form.
subject and body are the equivalent for text fields in a form.
i miss something may be in the data array or in the Content-Type but can't find the problem.
thanks for any help, regards, martin
From http://dtbaker.net/web-development/uploading-a-file-using-curl-in-php/:
notice the # infront of the file path, this is the magic part.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"#/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
Seems you were missing the magic part.
You may also switch from cURL to CakePHP's HttpSocket, but that's just personal prefrence.
http://book.cakephp.org/2.0/en/core-utility-libraries/httpsocket.html