This article follows up on PHP XML writing data to ABRA Flexi, which described writing to the address book using XML with structured data. Nowadays, however, XML is increasingly being replaced by JSON. This part of the guide shows you how to do it.
Prepare an array with the address book data.
$adresar = array( "winstrom" => array ( "adresar" => array( "nazev" => "CharlieB vložený jako JSON", "ulice" => "Lochotínská 18", "mesto" => "Plzeň", "psc" => "301 00", "tel" => "+420 371 124 321", "email" => "podporaflexi@abra.eu" ) ) );
You will send the data to the following URL:
// URL with xml data $url = "https://demo.flexibee.eu/c/demo/adresar.json";
If you change the URL extension from XML to JSON, ABRA Flexi will recognize that you are sending JSON and no additional headers will be required.
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($adresar));
Use the json_encode function and set the $adresar array as the postfields.
Those are all the changes you need to make in order to send data to ABRA Flexi in JSON format instead of XML.
The complete program will look like this, for example:
<?php
// URL with json data
$url = "https://demo.flexibee.eu/c/demo/adresar.json";
// create curl resource
$ch = curl_init();
// return content as a string from curl_exec
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// follow redirects (compatibility for changes in FlexiBee) //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// HTTP authentication curl_setopt($ch, CURLOPT_HTTPAUTH, TRUE);
// FlexiBee by default uses Self-Signed certificates //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// for debugging
// curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
// set username and password
curl_setopt($ch, CURLOPT_USERPWD, "winstrom:winstrom");
// set URL
curl_setopt($ch, CURLOPT_URL, $url);
// set HTTP method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
// json data array
$adresar = array( "winstrom" => array ( "adresar" => array( "nazev" => "CharlieB vložený jako JSON", "ulice" => "Lochotínská 18", "mesto" => "Plzeň", "psc" => "301 00", "tel" => "+420 371 124 321", "email" => "podpora@flexibee.eu" ) ) );
// set data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($adresar));
// execute
$output = curl_exec($ch);
// FlexiBee return value
header("Content-Type: application/json");
print ($output);
// close curl resource to free up system resources
curl_close($ch); ?>
