I am using a sms gateway i.e. sms4connect. Sms are sending successfully. Now the problem is I want to save the sms sending results into my table and then fetch the results.
Here is my php code :
<?php
$num = "92xxxxxxxxxx";
$mes = "Hello World (&) and a 5 note";
// Configuration variables
$type = "xml";
$id = "myid";
$pass = "mypass";
$lang = "English";
$mask = "mymask";
// Data for text message
$to = $num ;
$message = $mes;
$message = urlencode($message);
// Prepare data for POST request
$data = "id=".$id."&pass=".$pass."&msg=".$message."&to=".$to."&lang=".$lang."&mask=".$mask."&type=".$type;
// Send the POST request with cURL
$ch = curl_init('http://www.sms4connect.com/api/sendsms.php/sendsms/url');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); //This is the result from SMS4CONNECT curl_close($ch);
echo $result;
$query = mysql_query("INSERT INTO sms_response (phone, msg) values('$to','$result')");
?>
and I am getting the message "(300 Success Message Sent to Telecom. 239133150)". I want to save this message against each number into my database table. As you see in my PHP code that I am inserting $to, $result values in sms_response table but instead of saving number and message, I am getting these values in my table as shown in the image.
So How can I save message and Phone into my table correctly. Help would be highly appreciable.
You can create an XML object by this script
$xml=simplexml_load_string($result) or die("Error: Cannot create object");
print_r($xml);
In $xml you will get all the data fetched from the sms gateway. Which you can retrieve and save in DB.
simplexml_load_string it creates XML object from given XML string.
If you are getting a success message in node in your XML, you can simply retrieve the data like
$successMessage = $xml->message;
Save this $successMessage message in your db column. Please replace message with related field in your XML response. This is just an example.
Update
To store a phone number along with message you can concat them like
$successMessage = $num.": ".$xml->message;
This is just an example. You can concat them in any format. While retrieving the message if you want to get the phone number from the message you can explode the string by ":"
$msg = explode(":",$message_column);
echo $msg[0]; //phone number
echo $msg[1]; //message
Related
I have been trying to send the xml response I have stored in '$kxml' variable to the 'kycresult.php' page. I want to fetch the values from that xml and just print it. I am able to fetch the values if I store the xml in txt file and then get it using 'simplexml_load_file' but I don't want to create an extra file. Please let me know if there is a way to send the $kxml on next page.
$kycch = curl_init();
curl_setopt($kycch, CURLOPT_URL, $csckua_url);
curl_setopt($kycch, CURLOPT_POSTFIELDS, $kycxml);
curl_setopt($kycch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($kycch, CURLOPT_TIMEOUT, 20);
$kycresult = curl_exec($kycch);
$curl_errno = curl_errno($kycch);
$curl_error = curl_error($kycch);
curl_close($kycch);
// echo $kycresult;
$kxml = simplexml_load_string($kycresult);
if ($kxml['ret'] == 'Y') {
// $ksuccess = 'Authentication Successful';
header('location:Kycresult.php');
} else {
$ksuccess = 'Authentication Failed';
}
As one of the possible solutions you can store the variable in session
session_start();
$_SESSION['kxml'] = $kxml;
Then on the next page it will be available through
$kxml = $_SESSION['kxml'];
But actually it still stores in the auxiliary file by default under the hood.
I am updating some records using Curl, after sending a request on the response I am getting HTML formate data. I want to check the response that, what is the response.
My code is :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cacheManager = $objectManager->get('\Magento\Framework\App\CacheInterface');
$cacheManager->clean('catalog_product_' . $param);
$varnishurl = "www.exapmle.com";
$varnishcommand = "PURGE";
$productID = $param; // This is the Magento ProductID of the item you want to purge
$curl = curl_init($varnishurl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $varnishcommand);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['X-Magento-Tags-Pattern: catalog_product_'.$productID]);
$result = curl_exec($curl);
curl_close($curl);
OutPut:
I want to add condition to check what is returned ? either its purged like title of response or body text.I have tried by json formate sending but no luck.
Just change my code little bit and I got my answer. Add strip tags tag to remove HTML tags then search for what is required.
$data = strip_tags($result);
if(strpos($data, '200 Purged') !== false){
return 'Cache updated successfully for the Product Id '.$param;
}else{
return 'Some error occured during cache update for the Product Id '.$param;
}
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 would like to write messenger bot based on this script:
<?php
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
// Set this Verify Token Value on your Facebook App
if ($verify_token === 'testtoken') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
// Get the Senders Graph ID
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
// Get the returned message
$message = $input['entry'][0]['messaging'][0]['message']['text'];
//API Url and Access Token, generate this token value on your Facebook App Page
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<ACCESS-TOKEN-VALUE>';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
"recipient":{
"id":"' . $sender . '"
},
"message":{
"text":"The message you want to return"
}
}';
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request but first check if the message is not empty.
if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);
}
?>
All works correctly but i receive two responses to variable $message, for example:
Send "Hello";
$message = "Hello";
Receive message: "Hi";
$message = "Hi";
I would like to skip 3 and 4 points and receive only "Hello" message because i have to check if $message is my question or answer. Is it possible?
Greetings
You should skip any read and delivery messages, like this:
if (!empty($input['entry'][0]['messaging'])) {
foreach ($input['entry'][0]['messaging'] as $message) {
// Skipping delivery messages
if (!empty($message['delivery'])) {
continue;
}
// Skipping read messages
if (!empty($message['read'])) {
continue;
}
}
}
Or, you can deselect message_reads & message_deliveries checkboxes in Page Subscription section of your Facebook Page Settings/Webhooks.
I have a php curl script that my sms gateway provided to enable me send sms via xml.The original script is what i have below.
////////////////////////////// original php curl xml code from gate way
<?php
$user="smsgateway_user";
$pass="smsgateway_password";
$sender= "sendername";
$mobileno="2348034057037";
$message= "Your sms message goes here";
?>
<?php
$postUrl = "http://www.infobip.com/AddOn/SMSService/XML/XMLInput.aspx";
// XML-formatted data
$xmlString =
"<SMS>
<authentification>
<username>$user</username>
<password>$pass</password>
</authentification>
<message>
<sender>$sender</sender>
<text>$message</text>
</message>
<recipients>
<gsm>$mobileno</gsm>
</recipients>
</SMS>";
// previously formatted XML data becomes value of “XML” POST variable
$fields = "XML=" . urlencode($xmlString);
// in this example, POST request was made using PHP’s CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// response of the POST request
$response = curl_exec($ch);
// redirect the page upon successful sending
header("Location:customized/successfullysentbulksms.php");
curl_close($ch);
?>
/// code end
I how ever tried to tweak the codes in order to enable me send customized multiple sms by connecting to a mysql table with the following fields (id,name,mobileno) in such as way that i can select 10 recipients and send a customized message so that each recipient get the same message with his name showing in the message such as " Dear(.$name),thank you for visiting our store today"
From the little php that i know, i believe that i am suppose to connect to the database to select my recipient and then write a "do or while loop" that will enable the script to repeat or loop this function till it has successfully sent sms to all the recipients.
I'm presently stuck with embedding my loop function, please i will be glad if someone can take a look at what i've done so far and help me out.
My tweaked version of the code ///////////////////////////////////////////
<?php
$host="localhost"; // Host name
$username="user"; // Mysql username
$password="password"; // Mysql password
$db_name="db"; // Database name
$tbl_name="mysqltb"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Start looping rows in mysql database.
$row=mysql_fetch_array($result);
?>
<?
mysql_close();
?>
<?php
$mobileno = $row['mobileno'];
$name = $_row['name'];
$user="smsgateway_user";
$pass="smsgateway_password";
$sender= "sendername";
?>
<?php
$message = "you have received a customized bulk sms that is suppose to display your name";
$message2= "Dear ".$name." ".$message ;
$postUrl = "http://www.infobip.com/AddOn/SMSService/XML/XMLInput.aspx";
// XML-formatted data
$xmlString =
"<SMS>
<authentification>
<username>$user</username>
<password>$pass</password>
</authentification>
<message>
<sender>$sender</sender>
<text>$message2</text>
</message>
<recipients>
<gsm>$no</gsm>
</recipients>
</SMS>";
// previously formatted XML data becomes value of “XML” POST variable
$fields = "XML=" . urlencode($xmlString);
// in this example, POST request was made using PHP’s CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// response of the POST request
$response = curl_exec($ch);
// redirect the page upon successful sending
header("Location:customized/successfullysentbulksms.php");
curl_close($ch);
?>
Well there are a few things that I saw as I was looking through your code that were incorrect. The main things were 1) one of the $row variables was named $_row; 2) when you are looping through a query's return, you should use the standard pattern while ($row = mysql_fetch_array($result)), then each row will be looped through, instead you only fetched the first row.
Here is an example of what you wanted to do
<?php
$host = "localhost"; // Host name
$username = "user"; // Mysql username
$password = "password"; // Mysql password
$db_name = "db"; // Database name
$tbl_name = "mysqltb"; // Table name
$user = "smsgateway_user"; //sms user
$pass = "smsgateway_password"; //sms password
$sender = "sendername"; //sms sender name
$postUrl = "http://www.infobip.com/AddOn/SMSService/XML/XMLInput.aspx"; //XML Post url
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Retrieve data from database
$sql = "SELECT * FROM $tbl_name WHERE `send_status`=0";
$result = mysql_query($sql);
// Start looping rows in mysql database.
$totalCount = mysql_num_rows($result);
$successCount = 0;
while ($row = mysql_fetch_array($result))
{
$mobileno = $row['mobileno'];
$name = $row['name'];
$message = "Dear $name "; //Start message
$message .= "you have received a customized bulk sms that is suppose to display your name"; //append to message
// XML-formatted data
$xmlString =
"<SMS>
<authentification>
<username>$user</username>
<password>$pass</password>
</authentification>
<message>
<sender>$sender</sender>
<text>$message</text>
</message>
<recipients>
<gsm>$no</gsm>
</recipients>
</SMS>";
// previously formatted XML data becomes value of “XML” POST variable
$fields = "XML=" . urlencode($xmlString);
// in this example, POST request was made using PHP’s CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// response of the POST request
$response = curl_exec($ch);
//Might want to check the response here, see if it gives a true, or a 1 to say the message was sent successfully
if ($response)
{
$sql = "UPDATE `$tbl_name` SET `send_status`=1 WHERE `mobileno`='$mobileno' LIMIT 1";
$result = mysql_query($sql);
if (mysql_affected_rows($result) == 1) //success updating database
{
$successCount++;
}
}
// redirect the page upon successful sending
curl_close($ch);
}
if ($successCount == $totalcount)
header("Location:customized/successfullysentbulksms.php");
else
echo "Error Sending. $successCount out of $totalcount were successfully sent";
?>
Note: because this uses your database and sms provider, I haven't been able to test this code.
If you have any questions on how it works, I would be happy to answer them.
EDIT:
I have updated the code to include a mysql update statement to set the column 'send_status' to true after the message has been sent. I have also modified the mysql select statement to only get mobile numbers from the database that haven't been sent to yet (send_status is false).
Hope that helps