C2DM implementation PHP code - php

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.

Related

INVALID_TICKET in Zoho mail php Integration

I am using zoho email for sending email.
but when Trying the following code to send email ,
it returns the error invalid_ticket.
$data = array(
"fromAddress"=> "from#gmail.com",
"toAddress"=> "receive#gmail.com",
"subject"=> "Email - Always and Forever",
"content"=> "Email can never be dead. The most neutral and effective way, that can be used for one to many and two way communication.",
"askReceipt" => "yes"
);
// Convert the PHP array into a JSON format
$payload = json_encode($data);
// Initialise new cURL session
$ch = curl_init('https://mail.zoho.in/api/accounts/<user_id>/messages');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($payload))
);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
?>
Please help me to solve this. Thanks
This is not the right forum for this question but according to official documentation, you need to Kindly regenerate the authtoken with that generated authtoken which will work fine.
Source: https://help.zoho.com/portal/en/community/topic/zoho-cretor-getting-error-message-invalid-ticket-id-code-4834-when-trying-to-insert-records-in-zoho-crm

Calling WordPress REST API from server

I have an application server written in PHP, and it needs to interact with a WordPress deployment through WP's REST API. I am having difficulty calling the WP's REST API with authentication. For example, how do I create a user after authenticated with WP?
Most of the examples I found online are either using Ajax or cookies for authentication, which don't work in the server situation.
Can anyone suggest?
So you question is : how do I create a user after authenticated with WP?
I'm considering you don't have probem in the process of authentication.
If you use PHP you can use php file_get_contents function or cURL
bellow I will give you an example with PHP cURL
$post = array(
'username' => 'user2',
'email' => 'user2#somedomain.com',
'password' => 'somerandompassword');
$post = http_build_query($post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/wp/wp-json/wp/v2/users");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username" . ":" . "password"
);
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
print_r(json_decode($result, true), true);
You also can use file_get_contents function if your server can't use cURL for some reasons. You can find another another example of WP REST API with php in here www.wpsaga.info/tag/wp-rest-api

Backendless standalone cannot check isvalidusertoken REST 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>

Cannot send SMS through PHP API when hosted on OpenShift by Redhat

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.

SMS sending URL Not working With curl

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);

Categories