-1

I must send a post request with the following json data structure using PHP and cURL:

{
  "assistanceRequest":
        {
            "type": "TECHNICAL_DATA",
            "deliveryMode": "NORMAL",
            "creationDate": "2020-04-09T10:09:00+02:00",
            "source": "ATD",
            "language": "FR",
            "country": "FR"
        },
  "customer":
        {
             "code": 123456,
             "login": "client@company.com",
             "companyName": "Workshop Harris",
             "phoneNumber": "+44 123456789",
             "email": "workshop@company.com"
        },
}

However I don't understand how to create a similar request. I tried with the following code but I suppose it's not correct because I should group the data in two blocks:

    $post = [
        'type' => 'TECHNICAL_DATA',
        'deliveryMode' => 'NORMAL',
        'creationDate' => '2020-04-16T12:46:33+02:00',
        'source' => 'ATD',
        'language' => 'FR',
        'country' => 'CH',
        'code' => '123456',
        'login' => 'FR1234561A',
        'companyName' => 'Workshop Harris Love',
        'phoneNumber' => '+390432561543',
        'email' => 'client@client.com', ]; 

$post_encoded = json_encode($post);
$ch = curl_init('http://222.333.333.444/opafffws/public/api/req');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_encoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($post_encoded))                                                                       
);
$response = curl_exec($ch);
echo "The response 1: <br>" . $response;
echo "<br>";
curl_close($ch);

Can help?

Michele Della Mea
  • 966
  • 2
  • 16
  • 35

1 Answers1

0
<?php

$data = [
    'assistanceRequest' => [
        'type' => 'TECHNICAL_DATA',
        'deliveryMode' => 'NORMAL',
        "creationDate" => "2020-04-09T10:09:00+02:00",
        "source" => "ATD",
        "language" => "FR",
        "country" => "FR"
    ],
    'customer' => [
        "code" => 123456,
        "login" => "client@company.com",
        "companyName" => "Workshop Harris",
        "phoneNumber" => "+44 123456789",
        "email" => "workshop@company.com"
    ],
];

echo "<pre>"; print_r(json_encode($data));

This is an example for your structure. Tweak it as you need. The output of the above code will be:

{
  "assistanceRequest": {
    "type": "TECHNICAL_DATA",
    "deliveryMode": "NORMAL",
    "creationDate": "2020-04-09T10:09:00+02:00",
    "source": "ATD",
    "language": "FR",
    "country": "FR"
  },
  "customer": {
    "code": 123456,
    "login": "client@company.com",
    "companyName": "Workshop Harris",
    "phoneNumber": "+44 123456789",
    "email": "workshop@company.com"
  }
}
Harish ST
  • 1,475
  • 9
  • 23