After reading documentation I have tried to write the following source:
$contents = array();
//$contents['badge'] = "+1";
$contents['alert'] = "Touchdown!";
//$contents['sound'] = "cat.caf";
$notification = array();
$notification= $contents;
$audience['tag'] = "49ers";
$scheduled['scheduled_time'] = "2013-04-01T18:45:30";
$push['push'] = array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all");
$response = array();
array_push($response, array("name" => "My schedule", "schedule"=> $scheduled));
array_push($response, $push);
$json = json_encode($response);
//echo "Payload: " . $json . "\n"; //show the payload
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
// echo "Response: " . $content . "\n";
print_r($content);
// Check if any error occured
$response = curl_getinfo($session);
if($response['http_code'] != 202) {
$error = "Got negative response from server: " . $response['http_code'] . "\n";
} else {
$success = "Message has been sent successfully";
}
I am getting error = 0.
Can anyone help me please?
Thanks.
I have figured out the problem with the help of #dperconti (thank you very much).
I am posting a solution to 'how to schedule push for urbanairship' so others may find it helpful.
Code is:
define('APPKEY','xxxx'); // Your App Key
define('PUSHSECRET', 'xxxx'); // Your Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/schedules/');
$contents = array();
//$contents['badge'] = "+1";
$contents['alert'] = "Touchdown!";
//$contents['sound'] = "cat.caf";
$notification = array();
$notification= $contents;
$audience = "all";
$scheduled['scheduled_time'] = "2016-04-01T18:45:30";
// $push['push'] = array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all");
$response = array();
array_push($response, array("name" => "My schedule", "schedule"=> $scheduled, "push" => array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all")));
// array_push($response, $push);
$json = json_encode($response);
echo "Payload: " . $json . "<br><br><br>"; //show the payload
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
// echo "Response: " . $content . "\n";
print_r($content);
// Check if any error occured
$response = curl_getinfo($session);
if($response['http_code'] != 202) {
$error = "Got negative response from server: " . $response['http_code'] . "\n";
} else {
$success = "Message has been sent successfully";
}
curl_close($session);
Related
<?php
if(isset($_REQUEST['body']))
{
$body = $_REQUEST['body'];
$api_key = "tHx7zm7eb7z3yKHtNb";
$password = "x";
$yourdomain = "appicmobile";
$ticket_data = json_encode(array(
"body" => $body
));
$url = "https://$yourdomain.freshdesk.com/api/v2/tickets/32/reply";
$ch = curl_init($url);
$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $ticket_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($server_output, 0, $header_size);
$response = substr($server_output, $header_size);
if($info['http_code'] == 201)
{
// "Ticket created successfully, the response is given below \n";
//echo "Response Headers are \n";
//echo $headers."\n";
//echo "Response Body \n";
echo "$response";
}
else
{
if($info['http_code'] == 404)
{
echo "Error, Please check the end point \n";
}
else
{
echo "Error, HTTP Status Code : " . $info['http_code'] . "\n";
echo "Headers are ".$headers;
echo "Response are ".$response;
}
}
curl_close($ch);
}
else {
echo "error";
}
?>
While entering data through android using php shows empty.I am using volley to pass the data.
I'm trying to pass data from Android to my PHP application, but it seems to POST method is not working well, once all $_REQUEST variables are empty.
Tried all the method get,post but still not working
I have set up a Microsoft Outlook account for testing purposes. I have entered a couple of contacts and sent emails to/from that account, so it is clearly active. I have written php code to access that data, but nothing is returned. Everything else seems to be working in that the access tokens and verification all seems OK.
The php code is as follows (the 'xxxx' is obviously replaced with an actual code) with echos for diagnostic purposes, and some lines have been broken for clarity here.
<?php
//***************************************MSN
START********************************
$client_id = 'xxxxxx';
$client_secret = 'xxxxx';
$redirect_uri =
'http://localhost:8000/testcontacts/oauth_hotmail.php';
$urls_ = 'https://login.live.com/oauth20_authorize.srf?
client_id='.$client_id.
'&scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails&
response_type=code&redirect_uri='.$redirect_uri;
$msn_link = '<a href="'.$urls_.'" >MSN Contacts</a>';
echo $msn_link;
//***************************************MSN
ENDS********************************
?>
<?php
//function for parsing the curl request
function curl_file_get_contents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$client_id = 'xxxxx';
$client_secret = 'xxxxx';
$redirect_uri = http://localhost:8000/testcontacts/oauth_hotmail.php';
$auth_code = $_GET["code"];
echo '<br>' . $auth_code;
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,
'https://login.live.com/oauth20_token.srf');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$accesstoken = $response->access_token;
echo '<br>' . $accesstoken;
$url = 'https://apis.live.net/v5.0/me/contacts?
access_token='.$accesstoken.'&limit=100';
$xmlresponse = curl_file_get_contents($url);
$xml = json_decode($xmlresponse, true);
echo '<br>' . $url;
echo '<br>' . $xmlresponse;
echo '<br>' . $xml;
$msn_email = "";
foreach($xml['data'] as $emails)
{
// echo $emails['name'];
$email_ids = implode(",",array_unique($emails['emails'])); //will get
more email primary,sec etc with comma separate
$msn_email .= "<div><span>".$emails['name']."</span>
<span>". rtrim($email_ids,",")."</span></div>";
}
echo $msn_email;
?>
I get the following response (... represents long character string):-
MSN Contacts
Md9a1ad3b-d405-5f3f-1b08-10ae540b5669
EwAgA61DBAAUcSSzoTJJs.....
https://apis.live.net/v5.0/me/contacts?access_token=EwAgA61DBAAUcSSzo.....=&limit=100
{ "data": [], "paging": { } }
Array
Please can anyone tell me why nothing is returned. Thank you.
If anyone else comes this way, I have now got this to work. All the code that I have seen on the internet has been much more complicated than I wanted or refused to work at all, so not much use to me. Here I have managed to pare the code down to a minimum in a stand-alone working piece of code. I hope it works for you.
One question still remains. I cannot see how to get the contacts which are in a specific contact list, and not get all the contacts. Any ideas?
Here is the code...
<?php
//***************************************MSN START********************************
$client_id = 'xxxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxxxx';
$redirect_uri = 'https://.........php';
$login_uri = 'https://login.live.com/oauth20_authorize.srf';
$scope = '&scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails';
$urls_ = $login_uri;
$urls_ .= '?client_id='.$client_id;
$urls_ .= $scope.'&response_type=code';
$urls_ .= '&redirect_uri='.$redirect_uri;
$msn_link = '<a href="'.$urls_.'" >Get Contacts</a>';
echo $msn_link;
//***************************************MSN ENDS********************************
if(isset($_GET["code"])){
$auth_code = $_GET["code"];
// get contacts
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://login.live.com/oauth20_token.srf');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$accesstoken = $response->access_token;
$apiurl = "https://outlook.office.com/api/v2.0";
$headers = array(
"User-Agent: php-tutorial/1.0",
"Authorization: Bearer ".$accesstoken,
"Accept: application/json",
"return-client-request-id: true"
);
$search = array (
// Only return selected fields
"\$select" => "EmailAddresses,GivenName,Surname",
// Sort by GivenName
"\$orderby" => "GivenName ASC"
);
$outlookApiUrl = $apiurl . "/me/contacts?" . http_build_query($search);
$response = runCurl($outlookApiUrl, null, $headers);
$response = explode("\n", trim($response));
$response = $response[count($response) - 1];
$response = json_decode($response, true);
foreach ($response["value"] as $contacts) {
$email = $contacts["EmailAddresses"][0]["Address"];
echo '<br>' . $contacts["GivenName"] . ' ' . $contacts["Surname"] . ' ' . $email;
}
}
function runCurl($url, $post = null, $headers = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $post == null ? 0 : 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code >= 400) {
echo "Error executing request to Office365 api with error code=$http_code<br/><br/>\n\n";
echo "<pre>"; print_r($response); echo "</pre>";
die();
}
return $response;
}
?>
i have to add interswitch payment methods in my web application but i am receiving error
following is my code
function billersCategories()
{
$nonce=$randomNum=substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyz"), 0, 60);
$date = new DateTime();
$timestamp=$date->getTimestamp();
// Signature
$httpMethod = "GET";
$url='https://sandbox.interswitchng.com/api/v2/quickteller/categorys';
$clientId = "IKIA9D98ABCDEFGHIFAKEID1E09104959B9755C41E1";
$clientSecretKey = "d5uAr+U8QhSv8vQtKPDIUI62327Fsfsfsf65=";
$signatureCipher = $httpMethod."&".$url."&".$timestamp."&".$nonce."&".$clientId."&".$clientSecretKey;
$signature = base64_encode($signatureCipher);
$data = array("TerminalID" => "9APY556261");
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Authorization:InterswitchAuth SUtJQTUyNTBERkY1NkU5MzM2OUM0RkRBRjMxQTQ3QTg1RkNDODYyRTRDOUU=',
'Signature:'.$signature,
'Nonce:'.$nonce,
'Timestamp:'.$timestamp,
'SignatureMethod:SHA512'
));
$result = curl_exec($ch);
echo curl_getinfo($ch) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
var_dump($result);
}
But i am receiving following error
"The HTTP method is not supported for this resource", i tried http method POST but same error, i am new on API can someone please help me to solve this .
Use this instead, fill the following
In the variable
- $clientId
- $clientSecretKey
At the header
- TerminalID
<?php
$nonce=$randomNum=substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyz"), 0, 60);
$date = new DateTime();
$timestamp=$date->getTimestamp();
$httpMethod = "GET";
$clientId = "YOUR_OWN_ID";
$clientSecretKey = "YOUR_OWN_CLIENT_SECRET_KEY";
$resourceUrl='https://sandbox.interswitchng.com/api/v2/quickteller/categorys';
$resourceUrl = strtolower($resourceUrl);
$resourceUrl = str_replace('http://', 'https://', $resourceUrl);
$encodedUrl = urlencode($resourceUrl);
$transactionParams = "1";
$httpMethod = "GET";
$signatureCipher = $httpMethod . '&' . $encodedUrl . '&' . $timestamp . '&' . $nonce . '&' . $clientId . '&' . $clientSecretKey;
if (!empty($transactionParams) && is_array($transactionParams)) {
$parameters = implode("&", $transactionParams);
$signatureCipher = $signatureCipher . $parameters;
}
$signature = base64_encode(sha1($signatureCipher, true));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$resourceUrl);
// curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'Content-Type:application/json',
'Authorization:InterswitchAuth SUtJQTAzREM3RDY5NUREMzZFQURFNTQxNEE2Nzg1MUJCMUZFQ0Y5MUIxRjg=',
'Signature:'.$signature,
'Nonce:'.$nonce,
'Timestamp:'.$timestamp,
'SignatureMethod:SHA1',
'TerminalID:YOUR_ASSIGNED_TERMINAL_ID'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo $server_output;
?>
This worked for me, hope it help someone in future
I am trying to authenticate the google account using this tutorial. I am passing just my username and password but it returning false. I am new to it. Here is the function i am calling
function googleAuthenticate($username, $password, $source = 'your_google_app', $service = 'lh2') {
$session_token = $source . '_' . $service . '_auth_token';
// get an authorization token
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
$post_fields = "accountType=" . urlencode('GOOGLE')
. "&Email=" . urlencode($username)
. "&Passwd=" . urlencode($password)
. "&source=" . urlencode($source)
. "&service=" . urlencode($service);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request
curl_setopt($ch, CURLOPT_VERBOSE, true); // for debugging the request
var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT));
echo "<br>";
var_dump($post_fields);
echo "<br>";
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
exit();
if (strpos($response, '200 OK') === false) {
return false;
}
// find the auth code
preg_match("/(Auth=)([\w|-]+)/", $response, $matches);
if (!$matches[2]) {
return false;
}
$_SESSION[$session_token] = $matches[2];
return $matches[2];
}
I am making an Android location based app. For the location alert I want to send push notification in Android. But I am new to Urban airship. Can anyone tell me how can I implement it in PHP web API. It will be thankful to me
This is a good article that should get you started. It has both Android and iOS code as well.
Here is the code specifically for Android:
<?php
define('APPKEY','XXXXXXXXXXXXXXX'); // Your App Key
define('PUSHSECRET', 'XXXXXXXXXXXXXXX'); // Your Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
$contents = array();
$contents['alert'] = "PHP script test";
$notification = array();
$notification['android'] = $contents;
$platform = array();
array_push($platform, "android");
$push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform);
$json = json_encode($push);
echo "Payload: " . $json . "\n"; //show the payload
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
echo "Response: " . $content . "\n";
// Check if any error occured
$response = curl_getinfo($session);
if($response['http_code'] != 202) {
echo "Got negative response from server: " . $response['http_code'] . "\n";
} else {
echo "Wow, it worked!\n";
}
curl_close($session);
?>