• Resolved Sarvesh M Rao

    (@sarveshmrao)


    I tried every other way still the response I get from Discord is
    {"message": "Cannot send an empty message", "code": 50006}

    Actually, this is not a problem with Discord because my curl request works fine.

    Here’s my curl request snippet.

    
    function discordmsg($site, $errorcode, $desc, $webhook, $name){
      $msg = json_decode('{   "content": "Monitoring update, read more in the embed below! If you believe a false alarm has been triggered or something has gone wrong, email us ", "embeds": [ { "title": "Uptime Alert", "description": "Your site '.$site.' ('.$name.') is down, Expected response 200/302, Got response '.$errorcode.' (Which means '.$desc.')",
                        "color": 16711680, "footer": { "text": "This was given by the automatic monitoring system provided by example" } } ], "username": "Monitoring Update",
                        "avatar_url": "https://example.com/monitoring/monitoring.png" } ', true);
      if($webhook != "") {
        $ch = curl_init($webhook);
        $msg = "payload_json=" . urlencode(json_encode($msg))."";
        if(isset($ch)) {
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
          curl_setopt($ch, CURLOPT_POSTFIELDS, $msg);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          $result = curl_exec($ch);
          curl_close($ch);
          return $result;
        }
      }
    }
    

    Please tell me how this can be changed into wp_remote_post()

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi,

    Try the following:

    function discordmsg($site, $errorcode, $desc, $webhook, $name){
        $msg = json_decode('{   "content": "Monitoring update, read more in the embed below! If you believe a false alarm has been triggered or something has gone wrong, email us ", "embeds": [ { "title": "Uptime Alert", "description": "Your site '.$site.' ('.$name.') is down, Expected response 200/302, Got response '.$errorcode.' (Which means '.$desc.')",
                        "color": 16711680, "footer": { "text": "This was given by the automatic monitoring system provided by example" } } ], "username": "Monitoring Update",
                        "avatar_url": "https://example.com/monitoring/monitoring.png" } ', true);
    
        if($webhook != "") {
            wp_remote_post( $webhook, array( 'payload_json' => json_encode( $msg ) ) );
        }
    }
    • This reply was modified 3 years, 10 months ago by Dani Llewellyn. Reason: correctly quote payload_json
    Thread Starter Sarvesh M Rao

    (@sarveshmrao)

    @diddledan thanks for your reply.!

    
    function discordmsg($site, $errorcode, $desc, $webhook, $name){
        $msg = json_decode('
    {   
    "content": "Monitoring update, read more in the embed below! If you believe a false alarm has been triggered or something has gone wrong, email us ", 
    "embeds": [ 
    { 
    "title": "Uptime Alert", 
    "description": "Your site '.$site.' ('.$name.') is down, Expected response 200/302, Got response '.$errorcode.' (Which means '.$desc.')",
    "color": 16711680, 
    "footer": { 
    	"text": "This was given by the automatic monitoring system provided by example" 
    	} 
    } 
    ], 
    "username": "Monitoring Update" } ', true);
    
        if($webhook != "") {
            $response =wp_remote_post( $webhook, array( 'payload_json' => json_encode( $msg ) ) );
    
    if ( is_wp_error( $response ) ) {
        $errorResponse = $response->get_error_message();
        );
    } else {
        echo 'Response:<pre>';
        print_r( $response );
        echo '</pre>';
    }}}
    
    discordmsg('test site', '404', 'teset it desc', 'https://discord.com/api/webhooks/xxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'test name')
    

    What response I’m getting is

    
        [body] => {"message": "Cannot send an empty message", "code": 50006}
        [response] => Array
            (
                [code] => 400
                [message] => Bad Request
            )
    
    • This reply was modified 3 years, 10 months ago by Sarvesh M Rao. Reason: Removed a snippet of code which is not needed here
    Moderator bcworkz

    (@bcworkz)

    I think you want to pass array('body' => "payload_json=" . urlencode(json_encode($msg))) as 2nd arg to wp_remote_post().

    Thread Starter Sarvesh M Rao

    (@sarveshmrao)

    @bcworkz thank you so much it works perfectly.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to convert this curl request to wp_remote_post()?’ is closed to new replies.