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
<code><?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>';
}
?>
</code>
<code><?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>';
}
?>
</code>
<?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)
<code> [status_code] => 415
[protocol_version] => 1.1
[success] =>
[redirects] => 0
[url] => https://api.sprintsmsservice.com/api/SendSMS/
[history] => Array
(
)
[cookies] => WpOrgRequestsCookieJar Object
(
[cookies:protected] => Array
(
)
)
)
[filename:protected] =>
)
)
</code>
<code> [status_code] => 415
[protocol_version] => 1.1
[success] =>
[redirects] => 0
[url] => https://api.sprintsmsservice.com/api/SendSMS/
[history] => Array
(
)
[cookies] => WpOrgRequestsCookieJar Object
(
[cookies:protected] => Array
(
)
)
)
[filename:protected] =>
)
)
</code>
[status_code] => 415
[protocol_version] => 1.1
[success] =>
[redirects] => 0
[url] => https://api.sprintsmsservice.com/api/SendSMS/
[history] => Array
(
)
[cookies] => WpOrgRequestsCookieJar Object
(
[cookies:protected] => Array
(
)
)
)
[filename:protected] =>
)
)
Where i’m doing wrong? Because if i reload this file no SMS is sent to my number
2
I have fixed the problem, for future reference you might go through the codes below
<code>$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;
</code>
<code>$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;
</code>
$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;