谁能告诉我我将如何处理这种身体反应?

时间:2019-03-21 14:33:29

标签: wordpress wordpress-rest-api

我正在编写一个小插件,以通过其API将新的用户数据发送到Capsule CRM。我可以成功创建一个新的“ Person”对象和一个新的“ Task”,但是为了链接其中的两个,我必须按以下顺序进行操作:

  1. 将“派对”详细信息发送到Capsule以创建新的派对(通过wp_remote_post完成)
  2. 从该帖子中获取响应的正文,并从正文中获取部件ID
  3. 通过wp_remote_post将“任务”详细信息发送到Capsule,其中,该Party ID定义为从第一篇帖子的响应中获取的Party ID。

很多问题似乎是我无法从第一篇文章的正文回复中获得ID,因此该任务会在不与其关联的缔约方链接的情况下连续发布。

我的插件代码是最上面的代码片段,下面是一些有关Party响应显示方式的示例JSON。任何指针都将非常有帮助。

谢谢

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
    die;
}

//Find the details of a new user

add_action('um_registration_complete', 'send_doqaru_user', 10, 2);
//add_action ('um_registration_complete' , 'doqaru_redirect_home', 10, 2);

function send_doqaru_user ($user_id){


    $new_doqaru_user = get_userdata($user_id);
    $doqaru_user_email = $new_doqaru_user -> user_email;
    $doqaru_user_phone = $new_doqaru_user -> contact_number;



    // get all the meta data of the newly registered user
    $new_user_data = get_user_meta($user_id);

    // get the first name of the user as a string
    $doqaru_user_firstname = get_user_meta( $user_id, 'first_name', true );
    $doqaru_user_lastname = get_user_meta( $user_id, 'last_name', true );



    if ($doqaru_user_email) {
        error_log ($doqaru_user_email);
    }


    if( ! $new_doqaru_user ){
        error_log( 'Unable to get userdata!' );
        return;
    } 

    $url  = 'https://api.capsulecrm.com/api/v2/parties';
    $body = array(
        'party' => array(
        'type' => 'person',
        'firstName' => $doqaru_user_firstname,
        'lastName' => $doqaru_user_lastname,
        'phoneNumbers' => array(
            array(
                'type'      => 'Work',
                'number'    => $doqaru_user_phone
                )
            ),
        'emailAddresses' => array(
            array(
            'address'       => $doqaru_user_email,
            'type'          => 'Work'

    ))));

    $args = array(
        'method'      => 'POST',
        'timeout'     => 45,
        'sslverify'   => false,
        'headers'     => array(
            'Authorization' => 'Bearer Token goes here',
            'Content-Type'  => 'application/json',
        ),
        'body'        => json_encode($body),
    );

    $request = wp_remote_post( $url, $args ); 

    if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 201 ) {
        error_log( print_r( $request, true ) );
    }

    $response = wp_remote_retrieve_body($request);

    $bodyresponse = json_decode($response, true);

    $partyid = $bodyresponse ["party"]["id"];

    if ($partid) {
        error_log("the party ID is" . $partyid);
    } else {
        error_log ("Could not retrieve a Party ID");

    }


//CREATE THE NEW TASK WITH THE ID OF THE NEW USER
$taskurl = 'https://api.capsulecrm.com/api/v2/tasks';
$taskbody = array(
    "task" => array(
        "party"         => array(
            'id'        => $partyid,
            'type'      => 'person'),
        "description"   => "Follow up on new created user incase it is a lead",
        "dueOn"         => "2019-04-20"
        )
        );

$taskargs = array(
    'method'      => 'POST',
    'timeout'     => 45,
    'sslverify'   => false,
    'headers'     => array(
        'Authorization' => 'Bearer token goes here',
        'Content-Type'  => 'application/json',
    ),
    'body'        => json_encode($taskbody),
);

$taskrequest = wp_remote_post( $taskurl, $taskargs );


}



//REDIRECTION TO DOWNLOADS PAGE
function doqaru_redirect_home() {
    wp_redirect (home_url() . '/member-downloads');
    exit;
}

好吧,所以我稍微调整了一下代码,如果我var_dump $ body_response,那么我得到了下面的assoc数组。但是,当我error_log我的$ partyid时,它找不到值吗?

 array(1) {
  ["party"]=>
  array(18) {
    ["id"]=>
    int(182528177)
    ["owner"]=>
    NULL
    ["team"]=>
    NULL
    ["type"]=>
    string(6) "person"
    ["about"]=>
    NULL
    ["title"]=>
    NULL
    ["firstName"]=>
    string(6) "Daniel"
    ["lastName"]=>
    string(10) "Sutherland"
    ["jobTitle"]=>
    NULL
    ["createdAt"]=>
    string(20) "2019-03-29T15:02:33Z"
    ["updatedAt"]=>
    string(20) "2019-03-29T15:02:33Z"
    ["organisation"]=>
    NULL
    ["lastContactedAt"]=>
    NULL
    ["pictureURL"]=>
    string(59) "https://facehub.appspot.com/default/person?text=DS&size=100"
    ["phoneNumbers"]=>
    array(1) {
      [0]=>
      array(3) {
        ["id"]=>
        int(371335500)
        ["type"]=>
        string(4) "Work"
        ["number"]=>
        string(11) "07507681488"
      }
    }
    ["addresses"]=>
    array(0) {
    }
    ["emailAddresses"]=>
    array(1) {
      [0]=>
      array(3) {
        ["id"]=>
        int(371335501)
        ["type"]=>
        string(4) "Work"
        ["address"]=>
        string(24) "d.sutherland86@gmail.com"
      }
    }
    ["websites"]=>
    array(0) {
    }
  }
}

1 个答案:

答案 0 :(得分:0)

看看json_decode()如何构造您的JSON。 this.observable3.subscribe((...) => { ... this.transformShopList(); }); 导致:

var_dump($bodyresponse)

您要使用的object(stdClass)#5 (1) { ["parties"]=> array(1) { [0]=> object(stdClass)#1 (18) { ["id"]=> int(11587) ["type"]=> string(6) "person" ["about"]=> NULL ["title"]=> NULL ["firstName"]=> string(5) "Scott" ["lastName"]=> string(6) "Spacey" ["jobTitle"]=> string(17) "Creative Director" ["createdAt"]=> string(20) "2015-09-15T10:43:23Z" ["updatedAt"]=> string(20) "2015-09-15T10:43:23Z" ["organisation"]=> NULL ["lastContactedAt"]=> NULL ["owner"]=> NULL ["team"]=> NULL ["addresses"]=> array(1) { [0]=> object(stdClass)#2 (7) { ["id"]=> int(12135) ["type"]=> NULL ["city"]=> string(7) "Chicago" ["country"]=> string(13) "United States" ["street"]=> string(21) "847 North Rush Street" ["state"]=> string(2) "IL" ["zip"]=> string(5) "65629" } } ["phoneNumbers"]=> array(1) { [0]=> object(stdClass)#3 (3) { ["id"]=> int(12133) ["type"]=> NULL ["number"]=> string(12) "773-338-7786" } } ["websites"]=> array(0) { } ["emailAddresses"]=> array(1) { [0]=> object(stdClass)#4 (3) { ["id"]=> int(12134) ["type"]=> string(4) "Work" ["address"]=> string(22) "scott@homestyleshop.co" } } ["pictureURL"]=> string(64) "https://capsulecrm.com/theme/default/images/person_avatar_70.png" } } } 成为idint内的object中的array。您必须使用对象符号(object)来获得所需的信息:

->

Repl.it