API使用wp_Remote_POST获取空值

时间:2019-05-21 作者:Sanne Tuin

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 个回复
最合适的回答,由SO网友:MikeNGarrett 整理而成

数据的格式如下:

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