I have created a code for WordPress to integrate my custom SMS API. The API URL works fine when I type it on the URL bar; but when it in the code, the API URL will not call. So this code not working when the customer places an order:
add_action( 'woocommerce_order_status_processing','mysite_woocommerce_order_status_processing' );
function mysite_woocommerce_order_status_processing( $order_id ) {
$billing_phone="0729424391";
$message="yep";
$url=":9710/http/send-message?username=admin&password=admin&to=".$billing_phone."&messagetype=sms.automatic&message=".$message."'";
$response = file_get_contents( $url );
//print_r($response);
}
I have created a code for WordPress to integrate my custom SMS API. The API URL works fine when I type it on the URL bar; but when it in the code, the API URL will not call. So this code not working when the customer places an order:
add_action( 'woocommerce_order_status_processing','mysite_woocommerce_order_status_processing' );
function mysite_woocommerce_order_status_processing( $order_id ) {
$billing_phone="0729424391";
$message="yep";
$url="http://realcam.club:9710/http/send-message?username=admin&password=admin&to=".$billing_phone."&messagetype=sms.automatic&message=".$message."'";
$response = file_get_contents( $url );
//print_r($response);
}
Share
Improve this question
edited Oct 19, 2019 at 18:57
butlerblog
5,1013 gold badges26 silver badges43 bronze badges
asked Oct 19, 2019 at 17:28
madurangemadurange
1
5
|
1 Answer
Reset to default 1I would suggest using the HTTP API that WordPress provides for this purpose. Take a look at the GETting data from an API section for more details on how to get the data from your URL.
To get started, try:
Navigate to your
wp-config.php
file, and set theWP_DEBUG
andWP_DEBUG_LOG
constants to true (do NOT do this on the production server of your site).Next, replace your code with the following:
function mysite_woocommerce_order_status_processing( $order_id ) { $billing_phone = "0729424391"; // TODO Sanitize this input $message = "yep"; // TODO Sanitize this input $url = sprintf( "http://realcam.club:9710/http/send-message?username=admin&password=admin&to=%1$s&messagetype=sms.automatic&message=%2$s", $billing_phone, $message ); try { // GET the response. $response = wp_remote_get( esc_url( $url ) ); // If the response is a WP_Error object, jump to the catch clause. if ( is_wp_error( $response ) ) { throw new UnexpectedValueException( $response->get_error_message() ); } // Log the successful response to the debug log. error_log( print_r( $response, true ) ); } catch ( UnexpectedValueException $e ) { // Log the error to the debug log. error_log( $e ); } } add_action( "woocommerce_order_status_processing", "mysite_woocommerce_order_status_processing" );
and check what's in the
debug.log
file in thewp-content
directory of your WordPress installation.
This code will log the response or, if the request was unsuccessful, an error message to that file. I think it is straightforward enough for you to understand, but if you do have any questions or if anything goes wrong, please let me know.
init
and then exited at the end) and it worked fine - I get an "OK" message. What do you mean "the API URL will not call"? Are you getting an error and if so, what is the error? (What do you get as$response
?) – butlerblog Commented Oct 19, 2019 at 19:02