# Create contact with SDK and REST not working

**URL:** <https://integration.keap.com/t/create-contact-with-sdk-and-rest-not-working/77734>\
**Category:** API Q&A\
**Tags:** rest-api, php-sdk, contacts\
**Created:** [June 8, 2020, 1:03pm UTC](https://integration.keap.com/t/create-contact-with-sdk-and-rest-not-working/77734 "2020-06-08T13:03:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Beeston](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Matthew\_Beeston](https://integration.keap.com/u/Matthew_Beeston)\
**Post date:** [June 8, 2020, 1:03pm UTC](https://integration.keap.com/t/create-contact-with-sdk-and-rest-not-working/77734/1 "2020-06-08T13:03:51Z")

</div>

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](https://developer.infusionsoft.com/docs/rest/#!/Contact/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](https://developer.infusionsoft.com/docs/rest/#!/Contact/createContactUsingPOST) 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.

---

<div class="post-metadata">

**Author:** ![Andrew\_Johnson](https://sea1.discourse-cdn.com/flex019/user_avatar/integration.keap.com/andrew_johnson/32/1992_2.png) [@Andrew\_Johnson](https://integration.keap.com/u/Andrew_Johnson)\
**Post date:** [June 8, 2020, 9:55pm UTC](https://integration.keap.com/t/create-contact-with-sdk-and-rest-not-working/77734/2 "2020-06-08T21:55:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Matthew\_Beeston](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Matthew\_Beeston](https://integration.keap.com/u/Matthew_Beeston)\
**Post date:** [June 9, 2020, 7:12am UTC](https://integration.keap.com/t/create-contact-with-sdk-and-rest-not-working/77734/3 "2020-06-09T07:12:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Matthew\_Beeston](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Matthew\_Beeston](https://integration.keap.com/u/Matthew_Beeston)\
**Post date:** [June 15, 2020, 10:35am UTC](https://integration.keap.com/t/create-contact-with-sdk-and-rest-not-working/77734/4 "2020-06-15T10:35:53Z")

</div>

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

> [@Error When Trying To Create Contact with PHP SDK](https://integration.keap.com/t/error-when-trying-to-create-contact-with-php-sdk/10771/2):
>
> Hi @Jer_Fortenberry, I’m not extremely familiar with PHP but I think I may know your issue. The email\_addresses and phone\_numbers properties take an array of objects, each representing a different field or set of fields. I think you need to wrap your objects in one more array() like this. $contact = array( 'email\_addresses' =\> array( array( 'email' =\> 'test@test.com', 'field' =\> 'EMAIL1' ) ), 'given\_name' =\> 'John', 'family\_name' =\> 'Doe', 'phone\_numbers' =\> array( array( 'f…

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

```auto
{
	"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.
