0

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(
        array(
            '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:

{"id":72396659,"email":"info@domain.com","optInType":"Unknown","emailType":"Html","dataFields":[{"key":"BIRTHDAY","value":null},{"key":"CITY","value":null},{"key":"COUNTRY","value":null},{"key":"CUSTOMER_SINCE","value":null},{"key":"ENTRYDATE","value":null},{"key":"FIRSTNAME","value":null},{"key":"FULLNAME","value":null},{"key":"GENDER","value":null},{"key":"ICER_ESTIMATE","value":null},{"key":"ICER_LENGTH","value":null},{"key":"ICER_PANELS","value":null},{"key":"ICER_WIDTH","value":null},{"key":"LASTNAME","value":null},{"key":"LASTSUBSCRIBED","value":"2019-05-21T16:00:28"},{"key":"LB_IMAGEURL","value":null},{"key":"LB_IMAGEURL2","value":null},{"key":"LB_IMAGEURL3","value":null},{"key":"LB_ITEMNAME","value":null},{"key":"LB_ITEMNAME2","value":null},{"key":"LB_ITEMNAME3","value":null},{"key":"LB_ITEMPRICE","value":null},{"key":"LB_ITEMPRICE2","value":null},{"key":"LB_ITEMPRICE3","value":null},{"key":"ORDER_COUNT","value":null},{"key":"SID","value":null},{"key":"SQLID","value":null},{"key":"STATE","value":null},{"key":"TIMESTAMP","value":null},{"key":"TOTAL_SPENT","value":null},{"key":"ZIPCODE","value":null}],"status":"Subscribed"}

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
0

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.