I am using below code to send SMS by php curl. From browser when I am running $apiURL it works fine for me. But using below code of curl it's not working. OI gtting O/P as string(0) "". Am I doing something wrong,please help me.
$conturyCode = "91";
$client_mobile = "96******18";
$new_centername = "Apple Hospital";
$address = "Lal Darvaja,ringroad,surat(394220)";
$center_mobile = "86******91";
$apiURL = "http://103.16.101.52:8080/bulksms/bulksms?username=abc-def&password=abc123&type=0&dlr=1&destination=".$conturyCode.$client_mobile."&source=ABC&message=Scheduled at ".$new_centername." and ".$address." and ".$center_mobile;
// Initiate curl
$ch = curl_init();
//Set User client
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0");
curl_setopt($ch, CURLOPT_VERBOSE, true);
// Set The Response Format to Json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// Set Auto referer
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $apiURL);
// Execute
$result = curl_exec($ch);
// Check if any error occurred
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
}
// Closing
curl_close($ch);
var_dump($result);
Try the below code:
$message = urlencode("Scheduled at ".$new_centername." and ".$address." and ".$center_mobile);
$apiURL = "http://103.16.101.52:8080/bulksms/bulksms?username=abc-def&password=abc123&type=0&dlr=1&destination=".$conturyCode.$client_mobile."&source=ABC&message=".$message;
The message that you are passing needs to be url encoded.
I haven't used that specific API, but I would recommend going with Nexmo's SMS API. This API can be easily integrated in your app, increase your delivery rate, and bring you peace of mind from that you're using the SMS API gateway trusted by large brand name apps/companies around the world.
Nexmo, where I work, has an SMS API allows you to easily send SMS messages to phones in over 200 countries.
The API is extremely reliable, safe, & easy to integrate in your application.
All you need to do is make a simple HTTP call.
You can check the pricing for each country here
Below is some sample code in PHP that will allow you to start sending texts around the world. After signing up for the free trial, replace your own api_key, api_secret, to, from, and text (along with any other optional parameters you may want).
<?php
$url = 'https://rest.nexmo.com/sms/json?' . http_build_query([
'api_key' => API_KEY,
'api_secret' => API_SECRET,
'to' => YOUR_NUMBER,
'from' => NEXMO_NUMBER,
'text' => 'Hello from Nexmo'
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
Here's the documentation to send a message with Nexmo's SMS API
In my case below code worked successfully
$abc= 'http://bullet1.sdctechnologies.co.in:8080/api/mt/SendSMS?user='.$sms_username.'&password='.$sms_password.'&senderid='.$sms_sender_id.'&channel=Trans&DCS=0&flashsms=0&number='.$txt_mobile.'&text='.$message.'';
$url = str_replace(" ","%20",$abc);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
Related
I am trying to work out how to send push notifications to apple iOS devices from a php site. I had a php script that was working before the new http/2 way but since they do not support it anymore, it of course does not work.
The new script I have is this:
<?php
$ch = curl_init();
$device_token = 'correctDevice';
$pem_file = 'correctPemfile';
$pem_secret = 'correctCode';
$apns_topic = 'correctAppID';
//curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
curl_setopt($ch, CURLOPT_VERBOSE , true);
echo "Try 1 ================================================" . PHP_EOL;
//setup and send first push message
$url = "https://api.development.push.apple.com/3/device/$device_token";
curl_setopt($ch, CURLOPT_URL, "{$url}");
$sample_alert = '{"aps":{"alert":"hi #1","sound":"default"}}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch);
//var_dump($response);
//var_dump($httpcode);
echo "Try 2 ================================================" . PHP_EOL;
//setup and send second push message
$url = "https://api.development.push.apple.com/3/device/$device_token";
curl_setopt($ch, CURLOPT_URL, "{$url}");
$sample_alert = '{"aps":{"alert":"hi #2","sound":"default"}}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch);
//var_dump($response);
//var_dump($httpcode);
curl_close($ch);
?>
I've blocked out the device token, pem file, secret and app ID of course, but they are all correct.
The issue with it is that I get this error:
Warning: Use of undefined constant CURL_HTTP_VERSION_2_0 - assumed 'CURL_HTTP_VERSION_2_0' (this will throw an Error in a future version of PHP)
What could this issue be? I tried echoing phpinfo() and see if it supports http/2 and I found this:
From the documentation:
CURL_HTTP_VERSION_2_0 (int)
Available since cURL 7.33.0
Check the cURL version (e.g. by using phpinfo()).
I am using an incoming webhook on a web app to post some data to teams. I got this working in CURL after looking up some implementation methods, but it should be noted I have no experience in CURL. The thing is, it works about half the time, then the other half this comes up in the error log:
[01-Feb-2021 09:00:59 Europe/London] Error: Could not resolve host: outlook.office.com
Was hoping someone can take a look and see if there is something obvious I am doing wrong. This is the code used in my PHP file (with the webhook ID removed for privacy)
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_URL, 'webhook-id-here');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"text\": \"$teamsMessage\"}");
$headers = array();
$headers[] = 'Content-Type:application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
error_log("Error: " . curl_error($ch), 0);
}
curl_close($ch);
Looks like a DNS problem. Could not resolve host: outlook.office.com.
Maybe any firewall involved in blocking that? Remove CURLOPT_SSL_VERIFYPEER and CURLOPT_IPRESOLVE.
Try this simplified solution, which works well for me. I am using JSON header instead of form encoding.
// Paste the URL here
$url = 'https://outlook.office.com/webhook/XXX';
// Use the text encoded as MARKDOWN.
// Any markdown character needs to be escaped!
$body = ['text' => 'Hello World'];
$curlHandle = curl_init($url);
curl_setopt_array($curlHandle, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode($body)
]);
$response = curl_exec($curlHandle);
curl_close($curlHandle);
if ($response !== '1') throw new Exception('No Response from MS incoming webhook API');
I've been using the cloud environment, which works fine. I just recently downloaded the standalone version and am successfully running it on my ubuntu server. All the PHP SDK calls work, and the api/v1/data/[X Table Name] CuRL requests work.
However, I cannot get the CuRL request for valid login and logout to work. With the cloud implementation this was working:
function isValidToken($userToken){
$ch = curl_init();
$appId = APP_ID_FOR_CLOUD;
$restKey = REST_KEY_FOR_CLOUD;
$headers = array("application-id:$appId","secret-key:$restKey","application-type:REST");
curl_setopt($ch, CURLOPT_URL, "https://api.backendless.com/v1/users/isvalidusertoken/" . $userToken);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
This returns the proper response.
Now the only thing that changes are the IDs, keys, and URL, but it cannot find the requested URL. Here is the call to the standalone implementation:
function isValidToken($userToken){
$ch = curl_init();
$appId = APP_ID_STANDALONE;
$restKey = REST_KEY_STANDALONE;
$headers = array("application-id:$appId","secret-key:$restKey","application-type:REST");
curl_setopt($ch, CURLOPT_URL, "http://[my_server_ip_address]/v1/users/isvalidusertoken/" . $userToken);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
return $response;
}
The response I get is:
The requested URL /v1/users/isvalidusertoken/35A977A7-60DB-3772-FFC9-29E72AFAA200 was not found on this server.
Does anyone know how to resolve this issue? Thanks in advance!
I was just able to figure this out, the issue was due to a simple typo in the URL. For anyone who may make the same mistake, the standalone URL (for isvaliduesrtoken) is:
http://[my_server_ip_address]/api/<your_app_version>/users/isvalidusertoken/<user-token>
I have the following script which I used to send mobile verification sms through a third party sms gateway.
<?
$code = rand(100000, 999999);
$mobilenum = "+919898989898";
$data = '{"accountId": "xxxxxxxxxx","apiKey": "xxxxxxxxx","dndType": "transactional","smsType":"normal","senderId": "xxxxxx","to": "' . $mobilenum . '","templateId": "xxxxxxxx","placeholders": {"code": "' . $code . '"}}';
// Create a curl handle to domain 2
$url = "https://api.smsowl.in/v1/sms";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
//we want to get result as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//if servers support is and you want result faster, allow compressed response
//curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-type: application/json"
));
//configure a POST request with some options
curl_setopt($ch, CURLOPT_POST, true);
//put data to send
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "status code :" . $status;
//execute request
$result = curl_exec($ch);
curl_close($ch);
//show response form domain 2 to client if needed
//echo $code;
//print_r(apache_response_headers());
?>
The script used to work fine when it was hosted on hostgator server 2 months ago. But recently I started working on the application again and hosted it on OpenShift by RedHat since the hostgator service period ended. This script doesn;t work there. status code returns 0. curl_error() returns null and $result also returns null.
Please help.
I am creating the Android application where C2DM push notification is used. But i have a problem in creating the php code to use c2dm for sending messages. please guide me how to use the php code to send the messages. Actually there is a problem regarding this that how to get client auth token. I have seen the http://code.google.com/android/c2dm/index.html#server url but according to this i have created the android application and i got the registration id also and i also send to the user but how server uses this to send the application.
is there anything needed for the server from the android device to send the messages?.
To register your own server system and obtain the Authorise Tokens (this is what Cpt. Ohlund proposed):
function googleAuthenticate($username, $password, $source="Company-AppName-Version", $service="ac2dm") {
session_start();
if( isset($_SESSION['google_auth_id']) && $_SESSION['google_auth_id'] != null)
return $_SESSION['google_auth_id'];
// get an authorization token
$ch = curl_init();
if(!ch){
return false;
}
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
$post_fields = "accountType=" . urlencode('HOSTED_OR_GOOGLE')
. "&Email=" . urlencode($username)
. "&Passwd=" . urlencode($password)
. "&source=" . urlencode($source)
. "&service=" . urlencode($service);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// for debugging the request
//curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request
$response = curl_exec($ch);
//var_dump(curl_getinfo($ch)); //for debugging the request
//var_dump($response);
curl_close($ch);
if (strpos($response, '200 OK') === false) {
return false;
}
// find the auth code
preg_match("/(Auth=)([\w|-]+)/", $response, $matches);
if (!$matches[2]) {
return false;
}
$_SESSION['google_auth_id'] = $matches[2];
return $matches[2];
}
To send a message to a phone:
// $msgType: all messages with same type may be "collapsed": if multiple are sent,
// only the last will be received by phone.
function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) {
$headers = array('Authorization: GoogleLogin auth=' . $authCode);
$data = array(
'registration_id' => $deviceRegistrationId,
'collapse_key' => $msgType,
'data.message' => $messageText //TODO Add more params with just simple data instead
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
I've created an example in my blog in working with Android C2DM. I use Zend Framework and a custom component that I wrote out. It should give you the basic information that you will need in order to handle your Android C2DM implementation in PHP.
Android C2DM PHP w/ Zend Framework: http://blog.digitalstruct.com/2010/11/21/android-c2dm-with-php-and-zend-framework/
Regards,
Mike
Check this out: http://www.toppa.com/2010/google-clientlogin-php-example/
Otherwise I will get back to you, as I will try C2DM later this week.
As C2DM has been officially deprecated (google c2dm)
I recommend using the new GCM API as described in the following link:
GCM Php implementation
I have tried using the php code that was accepted as the correct answer but it is not working.
I am getting the response http code as "0".
I found the same code in the following link
Need help from the experts out here.