Send Message to phone using Codeigniter - php

I have a codeigniter base website and I have tried making forgot password by Phone and Email using Codeigniter framework to change password notification.And email is already working but I don't know how to send message to phone using codeigniter??
And one of my friends told me that use Curl function to send message to phone.But I didn't have any prior idea about CURL function so I searched on google but I couldn't figure out how to do this.
Would you please give me proper suggestion about how to send message to phone using codeigniter.
Any kind of help would be highly appreciated.
Thanks in advance.

Here is a simple code-igniter helper which you need to save as helpers/sendsms_helper.php
function sendsms($number, $message_body, $return = '0') {
$sender = 'SEDEMO'; // Can be customized
$smsGatewayUrl = 'http://springedge.com';
$apikey = '62q3xxxxxxxxxxxxxxxxxxxxxx'; // Need to change
$textmessage = urlencode($textmessage);
$api_element = '/api/web/send/';
$api_params = $api_element.'?apikey='.$apikey.'&sender='.$sender.'&to='.$number.'&message='.$textmessage;
$smsgatewaydata = $smsGatewayUrl.$api_params;
$url = $smsgatewaydata;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch);
curl_close($ch);
if(!$output){ $output = file_get_contents($smsgatewaydata); }
if($return == '1'){ return $output; }else{ echo "Sent"; }
}
How to use:
You can use below function anywhere in project to send sms:
Call sendsms function Ex. sendsms( '919918xxxxxx', 'test message' );
Please make sure to Load sendsms helper as $this->load->helper('sendsms_helper');

There are many companies that provide api for the same. I have used twilio for some time and find it reliable. The code for same can be
<?php
require "Services/Twilio.php";
$AccountSid = ""; //get from twilio
$AuthToken = ""; //get from twilio
$client = new Services_Twilio($AccountSid, $AuthToken);
$sms = $client->account->messages->sendMessage(
$tNumber, // Your twilio number
$number, // Number you want to send to
$message // Message you want to sms
);
Hope this helps. Again there are many other services out there also that you can look into

Related

How integrate free sms api in codeigniter?

$username = "info#example.com";
$hash = "*******************************************";
$test = "0";
$sender = "php sender";
$numbers = "7575757577";
$message = "verification code";
$message = urlencode($message);
$data = "username=".$username."&hash=".$hash."&message=".$message."&sender=".$sender."&numbers=".$numbers."&test=".$test;
$ch = curl_init('http://api.textlocal.in/send/?');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo"<pre>";
print_r($result);exit;
I am implementing free SMS API with CodeIgniter. Now, problem is that when I click on submit button it throws an error as mention below
{"errors":[{"code":3,"message":"Invalid login details"}],"status":"failure"}
I have no idea why it throwing this error. How can I implement this with CodeIgniter? I have also load curl library in autoload file. Please help me.
Thank You
I found a php class on the textlocal.in api documentation. Download that file and upload it to your site. Here is the download link; click.
Then just use this simple code to send a sms;
require 'textlocal.class.php';
$textlocal=new textlocal('me#textlocal.in','e215398a8820abd2c7a11a6cd5b1009d'); // email and hash
$textlocal->sendSms(['917788990011'],'Your car - KA01 HG 9999 - is due for service on July 24th, please text SERVICE to 92205 92205 for a callback','FORDIN'); // First target phone number, then the message, and then where the sms comes from
(Got the code from here; click)
If you have anymore questions, just ask :)

Search FB Messenger Message for Specific Word in PHP

So I'm trying to create a Facebook Messenger Chatbot, a very simple one. I have it working with a hardcoded response, but I want it to be able to read the senders message and respond in a specific way if it finds that word - like how chatbots should. I' trying to do so by using preg_match() but when I use my current code, the bot doesn't reply at all. Here's my code:
<?php
/**
* Webhook for Facebook Messenger Bot
*/
$access_token = "{mytoken}";
$verify_token = "{mytoken2}";
$hub_verify_token = null;
if (isset($_REQUEST['hub_challenge'])) {
$challenge = $_REQUEST['hub_challenge'];
$hub_verify_token = $_REQUEST['hub_verify_token'];
}
if ($hub_verify_token == $verify_token) {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
// perform a case-Insensitive search for the word "time"
if (preg_match('[hi|hello|sup]', $message)) {
$answer = "Hiya!";
}
else {
$answer = "IDK.";
}
// send the response back to sender
// 'text': 'Hiya!'
$jsonData = "{
'recipient': {
'id': $sender
},
'message': {
'text': $answer
}
}";
// initiate cURL.
$ch = curl_init("https://graph.facebook.com/v2.6/me/messages?access_token=$access_token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
if (!empty($input['entry'][0]['messaging'][0]['message'])) {
curl_exec($ch);
}
Do you know whether you actually receive messages from the Messenger Platform? Your webhook verification ends in an echo, where in fact you need to respond to the platform with a status code 200. You can also check your Facebook Apps dashboard to figure out whether your webhook is verified and being sent messages.
Documentation: https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup
Once you know that your webhook is verified and you are receiving messages, start with a fixed reply and then work towards dynamic responses. As #Toto suggested, adding logging will be very helpful to debug your code.

Integrating custom SMS API with woocommerce

So I have been working on a project to integrate custom SMS API with woocommerce and wc vendor plugins. Unfortunately, I didn't find any particular solution for this. Everyone was talking about some plugins who actually support existing gateways. I was wondering what if someone wants to integrate own api with woocommerce!
Finally, I have come up with own code which is given below. The code goes to function.php in your child theme. FYKI, I had to use rawurlencode to encode the text message as some telcos require encoded message.
Thank you.
Special thanks to: Integrating SMS api with woocommerce , Not sending messages
//DYNAMIC ORDER MSG TO CUSTOMER
add_action('woocommerce_order_status_processing', 'custom_msg_customer_process_order', 10, 3);
function custom_msg_customer_process_order ($order_id) {
//Lets get data about the order made
$order = new WC_Order($order_id);
//Now will fetch billing phone
$billing_phone = $order->get_billing_phone();
$billing_name = $order->get_billing_first_name();
$textmessage = rawurlencode("Dear $billing_name, Thank you for your order. Your order #$order_id is being processed. Please wait for confirmation call.");
// Now put HTTP SMS API URL
$url = "http://msms.THE_COMPANY.com/RequestSMS.php?user_name=YOUR_USER_NAME&pass_word=YOUR_PASSWORD&brand=YOUR_BRAND_NAME&type=1&destination=$billing_phone&sms=$textmessage";
// NOW WILL CALL FUNCTION CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
return $order_id;
}
I recently had this challenge myself, I had found an easy way to send notification SMS messages from WooCommerce with a plugin but I needed something to send messages in my own way.
I ended up using the Twilio service along with their API, I wrote up this tutorial as to how you can get it all set up so hope it helps!
function wpcodetips_send_sms( $smsMessage , $contactNumber ){
// Change to your account SID
$accountSID = 'XXXXXXXXXXXXX';
// Change to your account Auth Key
$authKey = 'XXXXXXXXXXXXX';
// Change to your account trial number
$sendNumber = '+XXXXXXXXXXX';
// The Twilio API Url
$url = "https://api.twilio.com/2010-04-01/Accounts/".$accountSID."/Messages.json";
// The data being sent to the API
$data = array(
'From' => $sendNumber,
'To' => $contactNumber,
'Body' => $smsMessage
);
// Set the authorisation header
$headers = array( 'Authorization' => 'Basic ' . base64_encode($accountSID . ':' . $authKey));
// Send the POST request and store the response in a variable
$result = wp_remote_post($url, array( 'body' => $data, 'headers' => $headers));
// Return the response body to ensure it has worked
return json_decode($result['body'], true);
}
https://www.wpcodetips.com/wordpress-tutorials/how-to-send-an-sms-from-wordpress-programmatically/
Not sure if this helps anyone but I recently set up a woocommerce shop where the owners are not able to check their email for orders all day as they are out on the field.
So with the mix of Hasan's and Gary's post right here I consolidated them into one to make a notification over SMS using Twilio when a new order comes in.
Just add the below to your functions.php and replace the accountSID, authKey, SendNumber and contactNumber values. And of course change the message to your preference.
Tested it with special characters like ÅÄÖ as well and it worked.
//Send SMS Notification to admin
add_action('woocommerce_order_status_processing', 'send_woo_order_sms', 10, 3);
function send_woo_order_sms ($order_id){
//Get order data
$order = new WC_Order($order_id);
//Get first name from order
$billing_first_name = $order->get_billing_first_name();
//Get last name from order
$billing_last_name = $order->get_billing_last_name();
// Change to your Twilio account SID
$accountSID = 'xxxxxxxxxxxxxxxx';
// Change to your Twilio account Auth Key
$authKey = 'xxxxxxxxxxxxxxxx';
// Change to your Twilio account number
$sendNumber = '+xxxxxxxxxxxxxxxx';
//Change to your verified caller number (receiver of the sms)
$contactNumber = '+xxxxxxxxxxxxxxxx';
//Message to send
$smsMessage = "$billing_first_name $billing_last_name has just placed an order. See order #$order_id.";
// The Twilio API Url
$url = "https://api.twilio.com/2010-04-01/Accounts/".$accountSID."/Messages.json";
// The data being sent to the API
$data = array(
'From' => $sendNumber,
'To' => $contactNumber,
'Body' => $smsMessage
);
// Set the authorisation header
$headers = array( 'Authorization' => 'Basic ' . base64_encode($accountSID . ':' . $authKey));
// Send the POST request and store the response in a variable
$result = wp_remote_post($url, array( 'body' => $data, 'headers' => $headers));
return $order_id;
}
I guess it could easily be changed to be sent out to the customer instead by changing the contactNumber to the billing phone.
$contactNumber = $order->get_billing_phone();
This would however require a paid plan at Twilio.
Ufone pakistan sms integration with woocommerce wordpress
if you are looking for integration with ufone pakistan sms api bsms ufone pakistan service provider with woocommerce wordpress then use the following code in your functions file
sms api integration ufone bsms with wordpress woocommerce
thanks to the author on this page
Integrating custom SMS API with woocommerce
//add this line for calling your function on creation of order in woocommerce
add_action('woocommerce_order_status_processing', 'custom_func', 10, 3);
function custom_func ($order_id) {
$order_details = new WC_Order($order_id);
//fetch all required fields
$billing_phone = $order_details->get_billing_phone();
$billing_name = $order_details->get_billing_first_name();
$billing_last_name = $order_details->get_billing_last_name();
$total_bill = $order_details->get_total();
$textmessage = rawurlencode("Dear $billing_name, Thank you for your order. Your order #$order_id is being processed. Please wait for confirmation call Total Bill = $total_bill");
// Now put HTTP ufone BSMS API URL
$url = "https://bsms.ufone.com/bsms_v8_api/sendapi-0.3.jsp?id=msisdn&message=$textmessage&shortcode=SHORTCODE&lang=English&mobilenum=$billing_phone&password=password&messagetype=Nontransactional";
// NOW WILL CALL FUNCTION CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
return $order_id;
}

SMS GATEWAY ISSUE in PHP

I am integrating SMS gateway for the very first time. I want to send sms when someone pays to the website. I am using the following code:
<?php
$pay="1000";
$msg="Arivind";
echo $url="http://yourdomainname.com/api/swsend.asp?username=xxxxxx&password=xxxxxx&sender=SENDERID&sendto=91XXXXXXXXX&message=Dear'$msg' Thanks for making payment of Rs '$pay'";
$c=curl_init();
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
curl_setopt($c,CURLOPT_URL,$url);
$contents=curl_exec($c);
curl_close($c);
echo "SMS Successfully sent";
?>
Now if i am using variable in body of message, the message is not sent but if i use static message the message is getting delivered to the number.
The static message doesnt solve my purpose as i need the message to be sent to different person, the variable used ie $msg will have different name of people & fetched from database.
KINDLY SUGGEST.
Using variables between single quotes ' does not convert it into dynamic values. Also its better to RestApi in simple PHP function:
function CURLcall($number, $message_body){
$api_params = "swsend.asp?username=xxxxxx&password=xxxxxx&sender=SENDERID&sendto=91XXXXXXXXX&message=$message_body";
$smsGatewayUrl = "echo $url="http://yourdomainname.com/api/";
$smsgatewaydata = $smsGatewayUrl.$api_params;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_URL, smsgatewaydata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
// Use file get contents when CURL is not installed on server.
if(!$output){
$output = file_get_contents($smsgatewaydata);
}
}
Call above function as:
$message_body = urlencode("Dear $msg Thanks for making payment of Rs $pay");
CURLcall('918954xxxxx',$message_body);
Please note: urlencode is useful to avoid errors in GET method as it convert space into encoded format http://php.net/manual/en/function.urlencode.php
You can also use http_build_query to convert your variables into a nicely formatted URL.
<?php
$fname = 'Matthew';
$lname = 'Douglas';
$amount = 1000;
$message = "Thanks for your payment of Rs {$amount}.";
$urlComponents = array(
'firstName' => $fname,
'lastName' => $lname,
'message' => $message
);
$url = 'http://yourdomainname.com/api/swsend.asp?';
echo $url . http_build_query($urlComponents);
?>

I want to know how sms gateway response work?How do I get the delivery report form sms APi?

MY SMS CODE: I have purchased the sms gate from third party.I am having some issue with that when I integrate in my website. I have listed my issue.can anyone guide me what I have to do further?Read my question?
<?php
$ID = 'xxxxxx';
$Pwd = 'xxxxx';
$PhNo = '1234567890,123456789';
$Text = 'welcome to US';
$url="http://t.dialmenow.info/sendsms.jsp?user=$ID&password=$Pwd&mobiles=$PhNo&sms=$Text&senderid =";
//echo $url;
$ret = file($url);
//echo $ret;
echo $ret[9];
?>
**I have problem with my message and delivery report.**
1.If you see the $Text variable $Text=welcome to US if I give space after first word the message is not coming to my mobile.
2.In api documentation they have given how to check delivery status. Here is the api delivery status code.they have given sample code. I want to know how to write the sample delivery status code for above php code.
http://t.dialmenow.info/getDLR.jsp?userid=username&password=password&messageid=1,2&externalid=1,2 &drquantity=X&fromdate=yyyy-mm-dd hh:mm:ss&todate=yyyy-mm-ddhh:mm:ss&redownload=yes&responcetype=xml
Explanation:
messageid=>When you send a message you will get an unique message id from API and you have to use this
messageid=>for getting the deliver status for that message.
externalid=>unique sms serial no which you will get in response.
Drquantity=>it means how many delivery status you want from Dialmenow application
Try encoding your message using the below
$msg = urlencode($Text)
Havent used file() but you could try implementing via cURL. Pls ensure that cURL in installed in the server where the code will be executed.
$ID = 'xxxxxx';
$Pwd = 'xxxxx';
$PhNo = '1234567890,123456789';
$Text = 'welcome to US';
$msg=urlencode($Text);
$url="http://t.dialmenow.info/sendsms.jsp?user=$ID&password=$Pwd&mobiles=$PhNo&sms=$msg&senderid=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER ,true);
$result = curl_exec($ch);
curl_close($ch);

Categories