I'm trying to post some data to an API and I keep getting null values in the fields.

My PHP code:

$url = 'https://apiconnector.com/v2/address-books/136517/contacts';
$username = 'apiuser';
$password = 'passwd';
$headers = array( 'Authorization' => 'Basic ' . base64_encode( "$username:$password" ), 'Content-Type' => 'application/json' );
$fields = array(
    'body' => json_encode(
            'EMAIL'     => 'info@domain.com',
            'dataFields' => array(
                'COUNTRY'     => 'TEST',
                'ICER_LENGTH'     => 'TEST',
                'ICER_WIDTH'     => 'TEST',
                'ICER_PANELS'     => 'TEST',
                'ICER_ESTIMATE'     => 'TEST'
    'headers' => $headers,
    'method'      => 'POST',
    'data_format' => 'body'

$response = wp_remote_post($url,$fields);

if ( is_wp_error( $response ) ) {
     $error_message = $response->get_error_message();
     echo "Something went wrong: $error_message";
} else {
     echo 'Response:<pre>';
     print_r( $response );
     echo '</pre>';

The response I get:


So the e-mail field gets there ok, but the rest do not. Is it maybe a problem with my 'dataFields' nested array?

The API documentation is in http://apiconnector.com/v2/help/wadl

Any help is appreciated!

  • The credentials and ID you posted here will allow anyone to delete all your data, so I hope this is a test account with dummy data. – MikeNGarrett May 21 at 17:21
  • Thanks for your answer. The $username and $password values are dummy, isn't that enough? – Sanne Tuin May 21 at 17:33
  • That would be enough, yes. I was just making sure. 👍 – MikeNGarrett May 21 at 17:51

It looks like the data needs to be formatted like this:

'DataFields' => array(
    array( 'Key' => 'COUNTRY', 'Value' => 'TEST' ),
  • 1
    This worked! Now I see how you got that from the response. I was getting overwhelmed with so many nested arrays. Thanks! – Sanne Tuin May 21 at 17:55
  • It's an odd pattern. I can see how this would be confusing. – MikeNGarrett May 21 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.