API getting null values with wp_remote_post

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'     => '[email protected]',
            '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":"[email protected]","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!

1 Answer
1

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

'DataFields' => array(
    array( 'Key' => 'COUNTRY', 'Value' => 'TEST' ),
);

Leave a Comment