Forked from patrickfreitasdev/smsportal-portal-forminator-integration.php
Created
July 13, 2023 00:29
-
-
Save alfredo-wpmudev/98afba8994cd40d4a21f5190c113b57d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Forminator smsportal integration ( Custom ) | |
* Description: This is a really simple example of integration, full doc https://docs.smsportal.com/recipes/send-smses-using-php | |
* Version: 1.0 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
// No need to do anything if the request is via WP-CLI. | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
add_action( 'plugins_loaded', array( 'forminator_custom_sms_postal_integration', 'instance' ) ); | |
class forminator_custom_sms_postal_integration { | |
private function __construct() { | |
add_action('forminator_form_after_save_entry', array( $this, 'send_sms' ), 20, 2 ); | |
} | |
public static function instance() { | |
static $instance = null; | |
if ( is_null( $instance ) ) { | |
$instance = new forminator_custom_sms_postal_integration(); | |
} | |
return $instance; | |
} | |
public function send_sms( $form_id, $response ) { | |
if( $response['success'] && $form_id == 1531 ){ | |
$apiKey = ''; | |
$apiSecret = ''; | |
$authToken = ''; | |
$accountApiCredentials = $apiKey . ':' .$apiSecret; | |
$base64Credentials = base64_encode( $accountApiCredentials ); | |
$authHeader = 'Authorization: Basic ' . $base64Credentials; | |
$authEndpoint = 'https://rest.smsportal.com/Authentication'; | |
$sendUrl = 'https://rest.smsportal.com/bulkmessages'; | |
// Field from Form | |
$phone_number = $_POST['phone-1']; | |
//$name = $_POST['name-1']; | |
$authOptions = array( | |
'http' => array( | |
'header' => $authHeader, | |
'method' => 'GET', | |
'ignore_errors' => true | |
) | |
); | |
$authContext = stream_context_create($authOptions); | |
$result = file_get_contents($authEndpoint, false, $authContext); | |
$authResult = json_decode($result); | |
$status_line = $http_response_header[0]; | |
preg_match('{HTTP\/\S*\s(\d{3})}', $status_line, $match); | |
$status = $match[1]; | |
if ($status === '200') { | |
$authToken = $authResult->{'token'}; | |
} | |
else { | |
error_log( print_r( $authResult, true ) ); | |
return; | |
} | |
$authHeader = 'Authorization: Bearer ' . $authToken; | |
$sendData = '{ "messages" : [ { "content" : "Hello SMS World from PHP", "destination" : "'. $phone_number . '" } ] }'; | |
$options = array( | |
'http' => array( | |
'header' => array("Content-Type: application/json", $authHeader), | |
'method' => 'POST', | |
'content' => $sendData, | |
'ignore_errors' => true | |
) | |
); | |
$context = stream_context_create($options); | |
$sendResult = file_get_contents($sendUrl, false, $context); | |
// Remove the comment in case need to debug the result | |
//error_log( print_r( $authResult, true ) ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment