Error when trying to Send SMS via WP_REMOTE POST

i'm facing a little problem here. I have these codes saved in send-sms.php file trying to setup an SMS API using wp_remote_post with my site. As part of my trial every time i run my codes below i get response that i don't understand. These are the codes <?php require_once 'wp-load.php'; $url = 'https://api.sprintsmsservice.com/api/SendSMS'; $response = wp_remote_post( $url, array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => array( 'api_id'=>'xxxxxx', 'api_password'=>'xxxxxx', 'sms_type'=>'T', 'encoding'=>'T', 'sender_id'=>'ChonryOTP', 'phonenumber'=>255714812XXX, 'templateid'=>null, 'textmessage'=>'Hello, This is a test message', ), 'cookies' => array() ) ); 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>'; } ?> And these are the results when i run the send-sms.php file (You can view full results in this LINK) [status_code] => 415 [protocol_version] => 1.1 [success] => [redirects] => 0 [url] => https://api.sprintsmsservice.com/api/SendSMS/ [history] => Array ( ) [cookies] => WpOrg\Requests\Cookie\Jar Object ( [cookies:protected] => Array ( ) ) ) [filename:protected] => ) ) Where i'm doing wrong? Because if i reload this file no SMS is sent to my number

Comment (1)

Jese Leos

12 hours ago

Verified user

I have fixed the problem, for future reference you might go through the codes below $api_url = 'https://xxxxxxxx/api/SendSMS/'; // Replace with your API URL $body = json_encode([ 'api_id'=>'xxxxxxxxxxx', 'api_password'=>'xxxxxxxxxxx', 'sms_type'=>'T', 'encoding'=>'T', 'sender_id'=>'xxxxx', 'phonenumber'=>'xxxxxx', 'templateid'=>null, 'textmessage'=>'Robson, This is test message' //'api_key' => $api_key, ]); $response = wp_remote_post($api_url, [ 'method' => 'POST', 'body' => $body, 'headers' => [ 'Content-Type' => 'application/json', ], ]); // Check for errors if (is_wp_error($response)) { error_log('SMS API Error: ' . $response->get_error_message()); return false; } // Handle the response $response_body = wp_remote_retrieve_body($response); $decoded_response = json_decode($response_body, true); if (isset($decoded_response['success']) && $decoded_response['success'] === true) { return true; // SMS sent successfully } error_log('SMS API failed with response: ' . $response_body); return false;

You’ll be in good company