Update developer does not work

Not applicable

I am trying to update a developer data in WordPress. The develoepr should be updated in both WordPress and Apigee. I have 2 ideas about the problem. First, maybe I am not adding attributes the right way or I do not use the right names for the textboxe, because If I try to print the content of the textboxes I do not get any string.I asked similar question some time ago, but than the problem was related with the array. And here I am using the same array as suggested before Could you give me any suggestions ot help? Thank you very much!!!!

I am getting the following:

body] => {
  "message" : "Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@5d598544; line: 1, column: 63] (through reference chain: com.apigee.developer.resource.Developer[\"attributes\"])",
  "contexts" : [ ]
}
    [response] => Array
        (
            [code] => 400
            [message] => Bad Request


Update WordPress user: 

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {


    /* Update user password. */
    if ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
        if ( $_POST['pass1'] == $_POST['pass2'] )
            wp_update_user( array( 'ID' => $current_user->ID, 'user_pass' => esc_attr( $_POST['pass1'] ) ) );
        else
            $error[] = __('The passwords you entered do not match.  Your password was not updated.', 'profile');
    }


    /* Update user information. */
    if ( !empty( $_POST['url'] ) )
        wp_update_user( array( 'ID' => $current_user->ID, 'user_url' => esc_url( $_POST['url'] ) ) );
    if ( !empty( $_POST['email'] ) ){
        if (!is_email(esc_attr( $_POST['email'] )))
            $error[] = __('The Email you entered is not valid.  please try again.', 'profile');
        elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
            $error[] = __('This email is already used by another user.  try a different one.', 'profile');
        else{
            wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
        }
    }


    if ( !empty( $_POST['first-name'] ) )
        update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first-name'] ) );
    if ( !empty( $_POST['last-name'] ) )
        update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last-name'] ) );
    if ( !empty( $_POST['description'] ) )
        update_user_meta( $current_user->ID, 'description', esc_attr( $_POST['description'] ) );


    /* Redirect so the page will show updated info.*/
  /*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
    if ( count($error) == 0 ) {
        //action hook for plugins and extra fields saving
        do_action('edit_user_profile_update', $current_user->ID);
        wp_redirect('http://localhost:81/DEMO/profile' );
        exit;
    }
}
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>">
        <div class="entry-content entry">
            <?php the_content(); ?>
            <?php if ( !is_user_logged_in() ) : ?>
                    -- .warning -->
            <?php else : ?>
                <?php if ( count($error) > 0 ) echo '<p class="error">' . implode("<br />", $error) . '</p>'; ?>
            
            
            <div class="regContainer">
                
                <p class="bigFonts"><b>Edit profile</b></p>
    <p class="smallFontsbold">Personal information</p>
    <p class="smallFonts"><b>Philips Hue values and respects your pravicy.</b></p>
                <form method="post" id="adduser" action="<?php the_permalink(); ?>">
                    <p class="form-username">
                        <label class="labeltextL" for="first-name"><?php _e(' <b>First Name</b>', 'profile'); ?></label>
                        <input class="text-input" name="first-name" type="text" id="first-name" value="<?php the_author_meta( 'first_name', $current_user->ID ); ?>" />
                    </p><!-- .form-username -->
                    <p class="form-username">
                        <label class="labeltextL" for="last-name"><?php _e('<b>Last Name</b>', 'profile'); ?></label>
                        <input class="text-input" name="last-name" type="text" id="last-name" value="<?php the_author_meta( 'last_name', $current_user->ID ); ?>" />
                    </p><!-- .form-username -->
                    <p class="form-email">
                        <label class="labeltextL" for="email"><?php _e('<b>E-mail</b> ', 'profile'); ?></label>
                        <input class="text-input" name="email" type="text" id="email" value="<?php the_author_meta( 'user_email', $current_user->ID ); ?>" />
                    </p><!-- .form-email -->
                    <p class="form-url">
                        <label class="labeltextL" for="url"><?php _e('<b>Website</b>', 'profile'); ?></label>
                        <input class="text-input" name="url" type="text" id="url" value="<?php the_author_meta( 'user_url', $current_user->ID ); ?>" />
                    </p><!-- .form-url -->
                    <p class="form-password">
                        <label  class="labeltextL" for="pass1"><?php _e('<b>Password </b>', 'profile'); ?> </label>
                        <input class="text-input" name="pass1" type="password" id="pass1" />
                    </p><!-- .form-password -->
                    <p class="form-password">
                        <label class="labeltextL" for="pass2"><?php _e('<b>Confirm Password </b>', 'profile'); ?></label>
                        <input class="text-input" name="pass2" type="password" id="pass2" />
                    </p><!-- .form-password -->
                   
         


                    <?php 
                        //action hook for plugin and extra fields
                        do_action('edit_user_profile',$current_user); 
                    ?>
                    <p class="form-submit">
                        <?php echo $referer; ?>
                        <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Save', 'profile'); ?>" />
                        <?php wp_nonce_field( 'update-user' ) ?>
                        <input name="action" type="hidden" id="action" value="update-user" />
                    </p><!-- .form-submit -->
                    </div>
                


APIGEE Developer











<?php
$current_user = wp_get_current_user();
 
    $current_user = wp_get_current_user();
    if ( !($current_user instanceof WP_User) )
         return;


//create a recourse link	
$urlbegin='https://api.enterprise.apigee.com/v1/organizations/trial/developers/';
$varemail= $current_user->user_email ;
$url= $urlbegin.$varemail;
$user= $current_user->user_login ;


$arg_data = array("email"=> $_POST['email'],  "firstName" =>$_POST['first-name'], "lastName" => $_POST['last-name']  , "userName" =>  $user ,
                  "attributes" => array( "name" => "xxx", "value" => "xxx"));


    
    
    print_r($_POST['email']);  ?><br>
<?php print_r($_POST['first-name']); ?><br>
 <?php print_r($_POST['last-name']); ?><br>
 <?php print_r( $user); ?><br>






<?php $data = json_encode( $arg_data );
$args = array(
     'method' => 'PUT','headers'=> array( 'Authorization' => 'Basic xxxxx', 'Content-type' => 'application/json'),'body'=> $data);
$response = wp_remote_request( esc_url_raw($url), $args );
	
	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>';
   
	 } <br>
Solved Solved
1 1 364
1 ACCEPTED SOLUTION

Hi Vanina,

I think the same problem is occurring here, as you had. last time. The developer attributes are an array-of-array.

This code illustrates how to add an attribute to a developer. It works for me, and may illustrate for you how to do what you want.

<?php

require_once("./wp-load.php");
include_once("./config.inc.php");

// contents of config.inc.php is like this: 
//  $config['org_name'] = 'my-apigee-org';
//  $config['username'] = 'apigee-user@example.com';
//  $config['password'] = 'VeeeeerySecret!';
//  $config['developer'] = 'developer@example.com';
//

$org_name = 'amer-demo2';
$urlbegin='https://api.enterprise.apigee.com/v1/o';

$url = $urlbegin . '/' . $config['org_name'] . '/' . 'developers';
$request_args = array(
  'method'  => 'GET',
  'headers' => array(
    'Authorization'  => "Basic " . base64_encode($config['username'] . ':' . $config['password'])
  )
);

$response = wp_remote_request( esc_url_raw($url), $request_args );

if ( is_wp_error( $response ) ) {
  $error_message = $response->get_error_message();
  echo "Something went wrong: $error_message";
}
else {
  echo 'Response:';
  print_r( $response );
}

print("=================================================================\n");
$url = $urlbegin . '/' . $config['org_name'] . '/' . 'developers' . '/' . $config['developer'];


$response = wp_remote_request( esc_url_raw($url), $request_args );


if ( is_wp_error( $response ) ) {
  $error_message = $response->get_error_message();
  echo "Something went wrong: $error_message";
}
else {
  echo 'Response:';
  //print_r( $response );
  print("# # # # # # # # # # # # # # # #\n");
  print_r( $response['body'] );


  $body = json_decode( $response['body'] , true );
  print("# # # # # # # # # # # # # # # #\n");
  print_r( $body['attributes'] );


  $objDateTime = new DateTime('NOW');

  // add a new attribute. 
  $body['attributes'][] = array(
    'name' => 'new attribute added ' . $objDateTime->format(DateTime::ISO8601),
    'value' => '42'
    );

  $request_args['method'] = 'PUT';
  $request_args['headers']['Content-type'] = 'application/json';
  $request_args['body'] = json_encode( $body );


  // update the developer
  $response = wp_remote_request( esc_url_raw($url), $request_args );
  print("# # # # # # # # # # # # # # # #\n");
  print_r( $response['body'] );
}


View solution in original post

1 REPLY 1

Hi Vanina,

I think the same problem is occurring here, as you had. last time. The developer attributes are an array-of-array.

This code illustrates how to add an attribute to a developer. It works for me, and may illustrate for you how to do what you want.

<?php

require_once("./wp-load.php");
include_once("./config.inc.php");

// contents of config.inc.php is like this: 
//  $config['org_name'] = 'my-apigee-org';
//  $config['username'] = 'apigee-user@example.com';
//  $config['password'] = 'VeeeeerySecret!';
//  $config['developer'] = 'developer@example.com';
//

$org_name = 'amer-demo2';
$urlbegin='https://api.enterprise.apigee.com/v1/o';

$url = $urlbegin . '/' . $config['org_name'] . '/' . 'developers';
$request_args = array(
  'method'  => 'GET',
  'headers' => array(
    'Authorization'  => "Basic " . base64_encode($config['username'] . ':' . $config['password'])
  )
);

$response = wp_remote_request( esc_url_raw($url), $request_args );

if ( is_wp_error( $response ) ) {
  $error_message = $response->get_error_message();
  echo "Something went wrong: $error_message";
}
else {
  echo 'Response:';
  print_r( $response );
}

print("=================================================================\n");
$url = $urlbegin . '/' . $config['org_name'] . '/' . 'developers' . '/' . $config['developer'];


$response = wp_remote_request( esc_url_raw($url), $request_args );


if ( is_wp_error( $response ) ) {
  $error_message = $response->get_error_message();
  echo "Something went wrong: $error_message";
}
else {
  echo 'Response:';
  //print_r( $response );
  print("# # # # # # # # # # # # # # # #\n");
  print_r( $response['body'] );


  $body = json_decode( $response['body'] , true );
  print("# # # # # # # # # # # # # # # #\n");
  print_r( $body['attributes'] );


  $objDateTime = new DateTime('NOW');

  // add a new attribute. 
  $body['attributes'][] = array(
    'name' => 'new attribute added ' . $objDateTime->format(DateTime::ISO8601),
    'value' => '42'
    );

  $request_args['method'] = 'PUT';
  $request_args['headers']['Content-type'] = 'application/json';
  $request_args['body'] = json_encode( $body );


  // update the developer
  $response = wp_remote_request( esc_url_raw($url), $request_args );
  print("# # # # # # # # # # # # # # # #\n");
  print_r( $response['body'] );
}