简体   繁体   中英

POST array through cURL doesn't give me the right format

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\" : \"Maria@Papakiriakou.com\",\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\" : \"Maria@Papakiriakou.com\",\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?

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM