Showing results for 
Search instead for 
Did you mean: 
In recognition of Martin Luther King Day, our Support Centers will be open with limited hours on Monday (1/20) from 10AM ET to 6PM ET.
Visit our Help Center to see all of the ways you can find answers to your questions.

API - PHP Add "state" field ("state" and/or "state_code") - Error


API - PHP Add "state" field ("state" and/or "state_code") - Error

Trying to add State to Contact via form.  Get Error below.  Other fields like "company_name" and "job_title" work fine.  Thanks!


Error Creating Contact

    [0] => Array
            [error_key] => json.field.invalid
            [error_message] => #/state: Property was found but is not permitted at this location.


Re: API - PHP Add "state" field ("state" and/or "state_code") - Error

Hi - In order for me to investigate and understand what's happening with the issue you're experiencing, please send along the JSON payload you're using to create the contact.


Rich Marcucella
Sr. Technical Writer, Content Developer
Web Services Team
CTCT Employee

Re: API - PHP Add "state" field ("state" and/or "state_code") - Error



Are you using our PHP SDK? Whether you are or not, this issue probably stems from the fact that address infromation is stored inside arrays, whereas most of the other contact information (like name and company) are stored NOT in arrays). If you're using our PHP SDK, below is a quick example of how to add the state. I would avoid the state_code field and use the 'state' field, since the user interface when you login to the account uses the state field to fill in the information.


//hard coding address values for simple testing
$street = "line 1 address";
$city = "city";
$state = "California";
$zip = "95758";

$contact = new Contact(); //only for new contacts
$contact->first_name = $_POST['first_name'];
$contact->last_name = $_POST['last_name'];
$contact->addAddress(Address::create( array("address_type"=>"BUSINESS","line1"=>$street,"city"=>$city,"state"=>$state,"postal_code"=>$zip)));
//change the above address fields to take information from user input


If you're not using the PHP SDK, you can take a look at our contact documentation pages to see how the state and other physical address fields should be wrapped inside a JSON array. 


Best Regards,

Shannon Wallace

Partner API Support Engineer


Re: API - PHP Add "state" field ("state" and/or "state_code") - Error

Yes, PHP SDK, sorry forgot to mention that.  Your response helped solve this issue.  There was one additional bit I needed to add in the AutoLoader include first:


use Ctct\Components\Contacts\Address;


Then, the final result for my successful code was:


$contact = new Contact();
            $contact->addList("999999999"); // I was hardcoding the List ID in mine so I replaced with 9s for demo.
            $contact->first_name = $_POST['Name'];
            $contact->company_name = $_POST['company_name'];
            $contact->job_title = $_POST['Title'];
            $contact->work_phone = $_POST['Phone'];
            $state = $_POST['State'];
            $contact->addAddress(Address::create( array("address_type"=>"BUSINESS","state"=>$state)));