最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp remote post - API getting null values with wp_remote_post

programmeradmin1浏览0评论

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

My PHP code:

$url = '';
$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

Any help is appreciated!

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/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/v2/help/wadl

Any help is appreciated!

Share Improve this question asked May 21, 2019 at 17:13 Sanne TuinSanne Tuin 233 bronze badges 3
  • 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 Commented May 21, 2019 at 17:21
  • Thanks for your answer. The $username and $password values are dummy, isn't that enough? – Sanne Tuin Commented May 21, 2019 at 17:33
  • That would be enough, yes. I was just making sure.
发布评论

评论列表(0)

  1. 暂无评论