Sending array to JSON

Please can somebody help me. I’m trying to send signup details using REST API but although I can send the email address I’m not sure how to send the additional ‘data fields’. This is the JSON output:

stdClass Object
(
    [id] => 123456789
    [email] => test@email
    [optInType] => Unknown
    [emailType] => Html
    [dataFields] => Array
        (
            [0] => stdClass Object
                (
                    [key] => FIRSTNAME
                    [value] => 
                )

            [1] => stdClass Object
                (
                    [key] => FULLNAME
                    [value] => 
                )

            [2] => stdClass Object
                (
                    [key] => GENDER
                    [value] => 
                )

            [3] => stdClass Object
                (
                    [key] => LASTNAME
                    [value] => 
                )
        )

and I’m trying to send the details like this:

$data = array(
    'Email' => 'test@email',
    'EmailType' => 'Html',
    'datafields' => array(
	'key' => 'FIRSTNAME',
	'value'=> 'Fred',
	'key' => 'LASTNAME',
	'value'=> 'Fred Smith',
	'key' => 'FULLNAME',
	'value'=> 'Fred Smith',
	'key' => 'GENDER',
	'value'=> 'Male',)
	);

but the datafields aren’t being sent.

Do you looking for json_encode?

Sorry I’m very new to this but yes this it’s using json_encode. I use it like this: json_encode($data);

The problem is I don’t know how to send the additional datafields though

I have no knowledge of this specifically, but are the field names case-sensitive as normal PHP variable names are? You use $datafields, it uses $dataFields? Wouldn’t explain why it sends the email correctly though. Or maybe:

$data = array(
    'Email' => 'test@email',
    'EmailType' => 'Html',
    'datafields' => array(
	'FIRSTNAME' => 'Fred',
	'LASTNAME' => 'Fred Smith',
	'FULLNAME' => 'Fred Smith',
	'GENDER' => 'Male')
	);

That’s a good idea but unfortunately it didn’t work :frowning:

Oops, I deleted my post since I think I misunderstood what you were asking! lol

I’ve just been told that the REST call is like this:

Authorization Basic YXBpdXNlci03MGQxYWE3OWQ3MWJAYXBpY29ubmVjdG9yLmNvbTpiYW5hbmFzMQ==
Content-Type application/json
{
"Email": "test@test.com,
"datafields": [
{
"key": "FIRSTNAME",
"value": "Bosswick"},
{
"key": "GENDER",
"value": "Invisible"}
],
}

but I still don’t know how to actually send that? I’m really desperate to get this sorted today and if I don’t I’m out of a job so I’d be so grateful if somebody could help me get this sorted please.

I can’t understand what is your problem actually.

You pass array to json_encode() and receive a string with JSON.
Don’t you know how to output that string or what?

$json = json_encode($data_array);
echo $json;

Or maybe you should provide more of your source code to help us understand where is your trouble and what you’re trying to achieve.

Also, from your original post:

This isn’t a JSON output. This is what var_dump() shows when you pass object returned by json_decode() to it. But you’re asking about encoding, not decoding. So I’m totally confused…

Sorry I’m very new to this and am now in a panic about it. The code I’ve got is:

<?php

/** POST EMAIL FIRST AND GET CONTACT FROM DOTMAILER */
$postContactUrl = 'https://apiconnector.com/v2/contacts/';
$data = array(
    'Email' => 'test@email',
    'EmailType' => 'Html',
    'dataFields' => array(
	'key' => 'FIRSTNAME',
	'value'=> 'Fred',
	'key' => 'LASTNAME',
	'value'=> 'Smith',
	'key' => 'FULLNAME',
	'value'=> 'Fred Smith',
	'key' => 'GENDER',
	'value'=> 'Male',)
	);
//post email and response will be contact object from dotmailer
$contact = execute_post($postContactUrl, $data);

/** ADD CONTACT TO ADDRESS BOOK */
$addContactToAddressBookUrl = 'https://apiconnector.com/v2/address-books/' . '123456' . '/contacts'; 

//post contact to address book and response will be address book object from dotmailer
$book =  execute_post($addContactToAddressBookUrl, $contact);

// echo "<pre>" . print_r($contact, true) . "</pre>";
 echo "<pre>" . print_r($book, true) . "</pre>";

function execute_post($url, $data){
    $requestBody = json_encode($data, true);

    $ch = curl_init();

    curl_setopt($ch, CURLAUTH_BASIC, CURLAUTH_DIGEST);
    curl_setopt($ch, CURLOPT_USERPWD, 'username' . ':' . 'password'); // credentials
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . 'application/json' ,'Content-Type: application/json'));

    $responseBody = json_decode(curl_exec($ch));

    curl_close($ch);

    return $responseBody;
}
?>

The email address is sent and excepted okay (a new contact is created). The problem is I’m also trying to add their name, gender, address etc to the contact.

The var_dump() (sorry I didn’t realise that wasn’t the JSON output) shows no data at all for any of the dataFields. This is what I’m trying to do: http://api.dotmailer.com/v2/help/wadl#AddressBookContacts (PostAddressBookContacts in particular).

Thank you so much for helping me with this.

I’ve just done a var_dump() on the $data and that does have the data (although it’s only the last field (GENDER) but for some reason it’s not being sent to dotmailer:

Array
(
    [Email] => hjfd@sd.com
    [EmailType] => Html
    [dataFields] => Array
        (
            [key] => GENDER
            [value] => Male
        )

)

You’re losing data in this code simply because you keep overwriting the ‘key’ and ‘value’ keys, so only the final assignment (the gender one) is surviving.

Looks like you need to make those datafields an array of arrays.

$data = array(
    'Email' => 'test@email',
    'EmailType' => 'Html',
    'dataFields' => array(
    array(
        'key' => 'FIRSTNAME',
	'value'=> 'Fred'),
    array(
	'key' => 'LASTNAME',
	'value'=> 'Smith'),
    array(
	'key' => 'FULLNAME',
	'value'=> 'Fred Smith'),
    array(
	'key' => 'GENDER',
	'value'=> 'Male'),
    )
);

I agree with @RavenVelvet
Also pay attention to uppercase letters (don’t actually know is it matters): DataFields, Key, Value - according to ApiContactData specification

Thank you so much that’s now working!! I genuinely can’t tell you how happy I am. I didn’t sleep last night I was so worried. Thank you so so so much. :smiley:

I’m so sorry I do have one further question. Some of the values are numeric but I don’t seem to be able to send them. Am I doing something wrong? I tried:

array(
    'Key' => 'TELEPHONE1',
	'Value'=> 012345),

and

array(
    'Key' => 'TELEPHONE1',
	'Value'=> '012345'),

but neither of those worked and didn’t contain any data at all

Is there a reason to structure the array that way? It could be simpler like

$data = array(
    'Email' => 'test@email',
    'EmailType' => 'Html',
    'dataFields' => array(
        'FIRSTNAME' => 'Fred',
	'LASTNAME' => 'Smith',
	'GENDER' => 'Male'
	'TELEPHONE' => '1234567'
    )
);

Also don’t see a reason why you would send the full name at all since you can construct it from the first name and last name at the receiving end.

It seems to be the way that the dotmailer guys have specified their api. I know, it doesn’t make sense to me either :wink:

Oh, okay then =)

I agree they’ve caused me enough headaches!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.