BuddyDev

Search

[Resolved] Send PUT remote request with BuddyPress checkbox

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #47550

    Hello my BuddyDev friends 🙂

    I would like to kindly ask you help in a problem that i am struggling.

    I am trying to create a code that when a BuddyPress user set active a Xprofile field checkbox a PUT remote request will trigger and update a single property of json endpoint, but i am not able to get it.

    Here is my code:

    function checkbox( $user_id, $posted_field_ids, $old_values, $new_values ) { 
    
    if ( $user_id == 12 ) {
    $playlistId = 462; // The ID of the playlist of the user.
    } 
    
    $argsPut = array( 
      'method' => 'PUT',
      'headers' => array(
        'Authorization' => 'Bearer {{32-CHARACTER ALPHANUMERIC KEY}}',
        'Content-Type' => 'application/json', 
      ),    
      'body' => $dataPut,
    ) ; 
    
    if ($old_values[103]['value'] !== $new_values[103]['value'] && !empty( $new_values[103]['value'] )) {
        
    $dataPut = wp_json_encode( array(  'is_enabled' => 'true' ) ); 
    $request = wp_remote_request( 'https://endpoint/api/' .  $playlistId, $argsPut  ) ; // i am sure that i am doing something wrong with wp_remote_request
    if( is_wp_error( $request ) ) {
        return false; // Bail early
    }
    } elseif ($old_values[103]['value'] !== $new_values[103]['value'] && empty( $new_values[103]['value'] )) {
    
    $dataPut = wp_json_encode( array(  'is_enabled' => 'false' ) ); 
    $request = wp_remote_request( 'https://endpoint/api/' .  $playlistId, $argsPut  ) ;
    if( is_wp_error( $request ) ) {
        return false; // Bail early
    }
    
    }
    
    }
    add_action( 'xprofile_updated_profile', 'checkbox', 10, 4 );

    An example value of the endpoint body is

    {
      "id": 0,
      "name": "Test Playlist",
      "type": "default",
      "source": "songs",
      "order": "shuffle",
      "remote_url": "https://remote-url.example.com/stream.mp3",
      "remote_type": "stream",
      "remote_buffer": 0,
      "is_enabled": true, -->>I only need to send a request and change this property from true to false 
      "is_jingle": false,
      "play_per_songs": 5,
      "play_per_minutes": 120,
      "play_per_hour_minute": 15,
      "weight": 3,
      "include_in_requests": true,
      "include_in_on_demand": true,
      "backend_options": "interrupt,loop_once,single_track,merge",
      "avoid_duplicates": true,
      "schedule_items": [
        "string"
      ]
    }

    I am pretty sure that for the BuddyPress hook, the code is fine, but i have been reading and reading for days, trying to understand how wp_remote_request actually works if i want to send a request and update one property on the endpoint side. Could you please help me?

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47558

    Hi,
    I am sorry, I skipped the post earlier.
    i am seeing a lost of issues:-

    1. Play list is not defined for users other than with id 12
    2. The Payload is not included as you are not putting it correctly.

    You should add the body parameter after you have created the $dataPut as shown below

    
    $argsPut['body'] = $dataPut;
    

    Please give it a try after that. I will suggest 2 other changes too:-

    1. add a fallback playlist id or return at the beginning
    2. make sure to check if ‘103’ exists in the $old_values/$new_values

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #47561

    Hello Brajesh,

    Thank you so much for your help 🙂

    1. Some specific playlists are related with specific users, that is why i have this condition at the beginning.

    2. After i have added the body parameter under the $dataPut it seems that i am one step better.
    Now when i activate the checkbox i get to see the change triggered at the endpoint (which is good,it means that my security headers are working + PUT is also working), but after if i inactivate the checkbox it doesn’t set 'is_enabled' => 'false'. After some tests, i have realized that it doesn’t matter what state the checkbox is. If i just save the field it always sends 'is_enabled' => 'true'. I am not sure where the problem is.

    I believe that there is no mistake in the conditions $old_values[103]['value'] !== $new_values[103]['value'] && empty( $new_values[103]['value'] ) and i double checked that the checkbox ID is 103.

    The code

    
    function checkbox( $user_id, $posted_field_ids, $old_values, $new_values ) { 
    
    if ( $user_id == 12 ) {
    $playlistId = 462; // The ID of the playlist of the user.
    } 
    
    $argsPut = array( 
      'method' => 'PUT',
      'headers' => array(
        'Authorization' => 'Bearer {{32-CHARACTER ALPHANUMERIC KEY}}',
        'Content-Type' => 'application/json', 
      ),    
    ) ; 
    
    if ($old_values[103]['value'] !== $new_values[103]['value'] && !empty( $new_values[103]['value'] )) {
        
    $dataPut = wp_json_encode( array(  'is_enabled' => 'true' ) ); 
    $argsPut['body'] = $dataPut; 
    $request = wp_remote_request( 'https://endpoint/api/' .  $playlistId, $argsPut  ) ; // i am sure that i am doing something wrong with wp_remote_request
    if( is_wp_error( $request ) ) {
        return false; // Bail early
    }
    } elseif ($old_values[103]['value'] !== $new_values[103]['value'] && empty( $new_values[103]['value'] )) {
    
    $dataPut = wp_json_encode( array(  'is_enabled' => 'false' ) );
    $argsPut['body'] = $dataPut;  
    $request = wp_remote_request( 'https://endpoint/api/' .  $playlistId, $argsPut  ) ;
    if( is_wp_error( $request ) ) {
        return false; // Bail early
    }
    
    }
    
    }
    add_action( 'xprofile_updated_profile', 'checkbox', 10, 4 );
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47570

    Hi
    Thank you for the reply.

    1. Can you please share the code for your endpoint as it seems you are probably checking the string value for checkbox incorrectly.

    2. I was not referring to any issue. My suggestion was to put an extra set of isset there to avoid notice if the field is not updated, this will cause php notice.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #47572

    Hello Brajesh,

    Thank you for your help 🙂
    1. The end point looks like this https://ibb.co/y6Dv0t5

    2. I am not sure how can i do that, could you please point out the code?

    – add a fallback playlist id or return at the beginning (unfortunately it is not possible to add a fallback playlist but what you mean by return at the beginning?)
    – make sure to check if ‘103’ exists in the $old_values/$new_values (i believe there is no need for this, since the ‘103’ will be always there)

    Thank you very much

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47606

    Thank you.
    The problem seems to to be the the use of ‘false’ instead of false.

    Try using boolean value instead of the string literal.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #47608

    Hello Brajesh,

    Thank you for your help 🙂
    That was the problem indeed.

    Thank you so much!!

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47627

    You are welcome.
    I am glad it is resolved.

The topic ‘ [Resolved] Send PUT remote request with BuddyPress checkbox’ is closed to new replies.

This topic is: resolved