Search code examples
phpjsonapicurlput

PHP, cURL, REST_API, JSON - Insert values in datafields using method "PUT"


I use the following code to insert values via API into certain fields of a database. The Method I have to use is PUT.

<?ph
header('Content-type: text/html; charset=utf-8');
error_reporting(E_ALL);
//ini_set('display_errors', '1');
$id = $_POST["id"];             
// URL to fetch
$url = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/42/elements/2155984";
// Setting the HTTP Request Headers
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';

$request_headers = array('Contect-Type:text/xml', 'Accept:text/xml');
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Content-type: application/json';
$request_headers[] = 'Accept-Encoding:  gzip, deflate, identity';
$request_headers[] = 'X-picturemaxx-api-key: K3f4e7m8dalksjdwdkkk';
$request_headers[] = "Authorization: Bearer token";


$dataj = array("classification_element_name" => 'string');
$data_json = json_encode($dataj);

 $ch = curl_init($url);
 // Set the url
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);

// Execute
$result = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Closing
curl_close($ch);

if ($code == 200) {
$result = json_decode($result, true);
print_r($result);
} else {
echo 'error ' . $code;
}
?>

Then I get the response

{
"request": {
"date": "2018-10-11T11:47:10+02:00",
"uid": "20181011-114710-03640bb2dcf2616d7"
},

So far so good. But unfortunately the values are not entered in the desired database field.

A Example-JSON-Body from the API looks like the following

{
  "classification_element_parent_id": 0,
  "classification_element_matchcode": "string",
  "classification_element_foreignref": "string",
  "localized": {
    "en-us": {
      "classification_element_name": "string"
    },
    "de-de": {
      "classification_element_name": "string"
    }
  }
}

A part of the JSON-Code looks like this

enter image description here

So how can I for example pass a value in the string classification_element_name that's part of the array item/localized/de-de?

I've tried this array before, too ->

$dataj['item']['localized']['de-de'] => 'string';

$data_json = json_encode($dataj);


Solution

  • You have to build an array like this:

    $dataj = array (
      'classification_element_parent_id' => 0,
      'classification_element_matchcode' => 'string',
      'classification_element_foreignref' => 'string',
      'localized' => 
      array (
        'en-us' => 
        array (
          'classification_element_name' => 'string',
        ),
        'de-de' => 
        array (
          'classification_element_name' => 'string',
        ),
      ),
    );