Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

posting data to an API

I’m working with a vendors API and the documentation is a little sparse.

they are looking for a post formatted like this.

{"import":{"members":[{"organization_customer_identifier":"XXXXXX","program_customer_identifier":"XXXXXX","member_customer_identifier":"XXX-XXX-XXXX","first_name":"Mister","last_name":"Tester","email_address":USER@DOMAIN.COM,"member_status":"OOOO","record_identifier":"XXX"}]}}'

my code is submitting it like this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{"members":{"organization_customer_identifier":"XXXXXX","program_customer_identifier":"XXXXXX","member_status":"OOOO","member_customer_identifier":"XXX-XXX-XXXX","first_name":"Mister","last_name":"Tester","email_address":"USER@DOMAIN.COM","record_identifier":"XXX"}}

I’m not that versed in using PHP to submit to an API with json/curl… as i understand it "import" is object and "members" is the array, i am missing the object. their examples have only shown expected results. their example results show multiple entries in the members array, i will only be submitting 1 member as they register. so i have no need to roll through an array of members.

here is my PHP.

$url = 'https://api-domain.com/import';
$ch = curl_init($url);
$data = array(
    'organization_customer_identifier' => 'XXXXX',
    'program_customer_identifier' => 'XXXXXX',
    'member_status' => 'OOOO',
    'member_customer_identifier' => 'PPPPPPPPPP',
    'first_name' => 'Mister',
    'last_name' => 'Tester',
    'email_address' => 'USER@DOMAIN.COM',
    'record_identifier' => 'XXX'
);
$payload = json_encode(array("import" => $data));

curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json', 'Access-Token: xxxxxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

thanks in advance for any direction on how to resolve this or correct my ignorance.

Sean

>Solution :

you’re missing the members array.

$data = ['members' => [[
    'organization_customer_identifier' => 'XXXXX',
    'program_customer_identifier' => 'XXXXXX',
    'member_status' => 'OOOO',
    'member_customer_identifier' => 'PPPPPPPPPP',
    'first_name' => 'Mister',
    'last_name' => 'Tester',
    'email_address' => 'USER@DOMAIN.COM',
    'record_identifier' => 'XXX'
]]];
$payload = json_encode(["import" => $data]);

To get the correspondence between JSON and PHP arrays, each [] or {} in the JSON corresponds to a PHP array, which is in [] in either case. {} corresponds to associative arrays; the keys are the same, but PHP uses => instead of :.

So when you see "members":[{ in the JSON, that corresponds to "members" => [[ in PHP.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading