Reputation: 509
I'm using swifdil api to create users from a html form via curl.
The API expects a certain format (json) to receive but I have no idea how I can achieve the format the API looks for.
Example code by the API:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://sandbox.swiftdil.com/v1/customers",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"type\" : \"INDIVIDUAL\",\r\n \"email\" : \"[email protected]\",\r\n \"first_name\" : \"Maria\",\r\n \"last_name\" : \"Papakiriakou\"\r\n}",
I need to submit the values through an HTML form so I did the following:
function createNewUser()
{
// First we get all the information from the fields we need to pass on to swiftdill.
$type = $_POST['type'];
$email = $_POST['email'];
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$fields = array(
'type' => $type,
'email' => $email,
'first_name' => $first_name,
'last_name' => $last_name
);
json_encode($fields);
$fields_string = http_build_query($fields);
$curl = curl_init();
// Set the options for the curl.
curl_setopt($curl, CURLOPT_URL, "https://sandbox.swiftdil.com/v1/customers");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURL_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer " .$newToken. "",
"Cache-Control: no-cache",
"Content-Type: application/json",
"Postman-Token: 0c513fa9-667d-4065-8531-8c4556acbc67"
));
The output of my code is as follows:
type=Individual&email=test%40mail.nl&first_name=John&last_name=Doe
Ofcourse this isn't formatted the way the api asks for it, namely:
CURLOPT_POSTFIELDS => "{\r\n \"type\" : \"INDIVIDUAL\",\r\n \"email\" : \"[email protected]\",\r\n \"first_name\" : \"Maria\",\r\n \"last_name\" : \"Papakiriakou\"\r\n}",
And also ofcourse, a cURL error appears as follows:
{"id":"xxxx-xxxx-xxxx-xxxx-xxxxxxxxx","type":"malformed_content","message":"Content of the request doesn't conform to specification"}
What do I need to do in my php code so that the API will accept my sent data?
Upvotes: 0
Views: 168
Reputation: 51
Change the line:
$fields_string = http_build_query($fields);
Into this:
$fields_string = json_encode($fields);
Because the API expecting JSON body, as you are sending non-JSON post body the API will don't know what is it and reject
Upvotes: 2
Reputation: 357
Why don't you try the response firstly via POSTMAN by setting the data as required by the API and test what actually the API needs.
As per the information provided, it looks like you should remove the below line and just post json_encoded data.
$fields_string = http_build_query($fields);
Upvotes: 0