Create contact with SDK and REST not working

Hi there,

I am trying to create a contact from an array using the PHP-SDK.

I have already got an oauth2 token and retrieved all Keap/Infusionsoft contacts, lists of custom_fields and tags. Then I can update contacts with: $infusionsoft->contacts->mock($x)->save(); and tags with $infusionsoft->contacts->find($id)->addTags(array($x));.

But I have a problem when I try and add a new contact. I have searched the forums and docs but cannot find any answers. I am using the following code:

$template = array();
$template['addresses'] = array();
$template['anniversary'] = '';
$template['birthday'] = '';
$template['company'] = array();
$template['contact_type'] = '';
$template['custom_fields'] = array();
$template['email_addresses'] = array();
$template['family_name'] = '';
$template['fax_numbers'] = array();
$template['given_name'] = '';
$template['job_title'] = '';
$template['lead_source_id'] = '';
$template['middle_name'] = '';
$template['opt_in_reason'] = '';
$template['origin'] = array();
$template['owner_id'] = 0;
$template['phone_numbers'] = array();
$template['preferred_locale'] = 'en_GB';
$template['preferred_name'] = '';
$template['prefix'] = '';
$template['social_accounts'] = array();
$template['source_type'] = 'API';
$template['spouse_name'] = '';
$template['suffix'] = '';
$template['time_zone'] = '';
$template['website'] = '';

$query = array('email_addresses') => 'test@email.com', 'field' => 'EMAIL1');
array_merge($template, $query);

$infusionsoft->contacts()->create($query, true);

It doesn’t matter if I omit the array_marge operation, the result is the same:

Client error: `POST https://api.infusionsoft.com/crm/rest/v1/contacts/?access_token=xxx` resulted in a `400 Bad Request` response: {"message":"Input could not be converted to a valid request"}

I have tried several variations but get the same error message every time:

$infusionsoft->contacts->mock($query)->save();
$infusionsoft->contacts()->create($query, true);
$infusionsoft->contacts()->create($query);
$infusionsoft->contacts->create($query, true);
$infusionsoft->contacts->create($query);

I have tried converting the array (with and without the merge as above) into JSON and using it here: createContactUsingPOST, but I get the following message:

Response Code: 0
Response Headers : "error": "no response from server"

Even if I edit the body sample from here and past it back into the contact input I get the above error again.

How am I formatting the data wrong? Any solution would be greatly appreciated.

Hey @Matthew_Beeston,

I think the issue is around the line where you are doing array_merge. I think instead of that line you want

$template['email_addresses'][] = $query;

The reason beling when you are doing the array merge, it is just appending “email” and “field” to the $template variable, instead of nesting them in the ‘email_addresses’ array.

1 Like

Thanks for your reply @Andrew_Johnson.

I made a mistake when making that code snippet (edited the original post now), my actual code does merge the data correctly and produces the following output (formatted as JSON for ease).

{
	"addresses": {
		"0": {
			"field": "BILLING",
			"line1": "11 xxxxxxx xxxxxx",
			"locality": "xxxxxxxxxxx",
			"postal_code": "xxxx xxx",
			"country_code": "xxx"
		}
	},
	"anniversary": "",
	"birthday": "xxxx-xx-xx",
	"company": {},
	"contact_type": "",
	"custom_fields": {
		"0": {
			"content": "xxx",
			"id": 45
		},
		"1": {
			"content": "2020-06-08",
			"id": 61
		},
		"2": {
			"content": "",
			"id": 3
		},
		"3": {
			"content": "xxxxx",
			"id": 5
		},
		"4": {
			"content": "xxxxx",
			"id": 11
		},
		"5": {
			"content": "xxxxx",
			"id": 9
		},
		"6": {
			"content": "",
			"id": 13
		},
		"7": {
			"content": "250",
			"id": 49
		},
		"8": {
			"content": "0",
			"id": 47
		},
		"9": {
			"content": "5",
			"id": 21
		},
		"10": {
			"content": "xxxxx",
			"id": 55
		},
		"11": {
			"content": 1,
			"id": 25
		},
		"12": {
			"content": 0,
			"id": 23
		},
		"13": {
			"content": 0,
			"id": 27
		},
		"14": {
			"content": 0,
			"id": 29
		},
		"15": {
			"content": 1,
			"id": 31
		},
		"16": {
			"content": 1,
			"id": 33
		},
		"17": {
			"content": 1,
			"id": 35
		},
		"18": {
			"content": 0,
			"id": 37
		},
		"19": {
			"content": 0,
			"id": 39
		},
		"20": {
			"content": "2020-06-08",
			"id": 61
		}
	},
	"email_addresses": {
		"email": "name@email.com",
		"field": "EMAIL1"
	},
	"email_status": "SingleOptIn",
	"family_name": "xxxxxx",
	"fax_numbers": {},
	"given_name": "xxxxx",
	"job_title": "",
	"lead_source_id": "",
	"middle_name": "",
	"opt_in_reason": "xxxx",
	"origin": {},
	"owner_id": 0,
	"phone_numbers": {
		"0": {
			"type": "Home",
			"field": "PHONE1",
			"number": "xxxxx"
		},
		"1": {
			"type": "Mobile",
			"field": "PHONE2",
			"number": "xxxxx"
		},
		"2": {
			"type": "Work",
			"field": "PHONE3",
			"number": "xxxxx"
		}
	},
	"preferred_locale": "en_GB",
	"preferred_name": "",
	"prefix": "",
	"social_accounts": {},
	"source_type": "API",
	"spouse_name": "",
	"suffix": "",
	"time_zone": "",
	"website": ""
}

Even if I omit the template data and use the bare minimum input with the following JSON I still get the same error.

{
	"email_addresses": {
		"email": "name@email.com",
		"field": "EMAIL1"
	}
}

Any ideas?

I found an answer after rereading @Andrew_Johnson’s post and finding this post in the forums:

In my case the email_addresses field needed to be a multidimensional array. I corrected my code to produce the following output:

{
	"email_addresses": {
		"0": {
			"email": "name@email.com",
			"field": "EMAIL1"
		}
	}
}

It would be extremely helpful if somewhere between the SDK and the API could give more detailed feedback as to which element of the input is invalid.