I have created development app in urbanairship and trying to send push notification from server using PHP code.
My code:
define('APPKEY','xxx'); // Your App Key
define('PUSHSECRET', 'ytyy'); // Your Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
$notification = array();
$notification['alert'] = "alert";
$platform = array();
array_push($platform, "android"); //comment out if you don't want Android
$richpush = array();
$richpush['title'] = "title";
$richpush['body'] = $message;
if(strlen($deviceToken)>50){
$deviceToken=str_replace(" ","",$deviceToken);
$push = array("audience"=>array("device_token"=>$deviceToken), "notification"=>$notification, "device_types"=>$platform, "message"=>$richpush);
}else{
$deviceToken=str_replace(" ","-",$deviceToken);
$push = array("audience"=>array("apid"=>$deviceToken), "notification"=>$notification, "device_types"=>$platform, "message"=>$richpush);
}
$json = json_encode($push);
$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";
But I'm getting :
Response: {"ok":false,"error":"The specified appkey [xxx] is not entitled to use the Message Center.","error_code":403}
Any help would be appreciated.
Message Center is not included in the Start or Basic accounts. Contact support and they will work with you to add Message Center entitlements.
Related
I'm trying to learn the shopify api for a project. Tried a few pasted codes on their forum, adapting my code to theirs.
The documentation says to do the authentication this way, by providing the following:
https://{username}:{password}#{shop}.myshopify.com/admin/api/{api-version}/{resource}.json
{username} — The API key that you generated
{password} — The API password
{shop} - The name that you entered for your development store
{api-version} — The supported API version you want to use
{resource} — A resource endpoint from the REST admin API
I'm trying to do a GET request on all the orders made on the store.
/ info
$API_KEY = '75c89bf******ea919ce****7702';
$PASSWORD = '2061******82d2802**f***9403';
$STORE_URL = '*****-na-***-c***.myshopify.com';
$AUTHORIZATION = base64_encode($API_KEY . ':' . $PASSWORD);
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . '/admin/api/2020-01/orders.json';
$header = array();
$header[] = 'Accept: application/json';
$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: Basic ' . $AUTHORIZATION;
$session = curl_init();
//options
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_HTTPHEADER, $header);
curl_setopt($session, CURLOPT_GET, 1);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
//exec
$response = curl_exec($session);
curl_close($session);
print_r($response);
// error
if($response === false)
{
print_r('Curl error: ');
}
The code doesn't work at all, without any error code, completly blank, with only the first project echo showing.
I verified my API keys and they are working, I can insert them manually into chrome.
You don't need the header for authorization. Your code should like:
$API_KEY = '75c89bf******ea919ce****7702';
$PASSWORD = '2061******82d2802ff***9403';
$STORE_URL = 'porcao-na-sua-casa.myshopify.com';
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . '/admin/api/2020-01/orders.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE) //can check status code, requst successfully processed if return 200
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Found the problem when I ran in terminal, did not install php-curl on this machine.
#Kshitij solution worked, just like mine, when curl was properly installed.
// Provide the required parameters like store url , endpoint etc.
function shopifyApiCall($STORE_URL ,$query = [], $endpoint, $API_KEY, $PASSWORD){
//API_Key : your API key, you can get it from your APP setup.
//Password : Your Access Token
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . $endpoint;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode($query));
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$error_number = curl_errno($ch);
$error_message = curl_error($ch);
curl_close($ch);
// Return an error is cURL has a problem
if ($error_number) {
return $error_message;
} else {
// No error, return Shopify's response by parsing out the body and the headers
$response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
// Convert headers into an array
$headers = array();
$header_data = explode("\n",$response[0]);
$headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
array_shift($header_data); // Remove status, we've already set it above
foreach($header_data as $part) {
$h = explode(":", $part);
$headers[trim($h[0])] = trim($h[1]);
}
// Return headers and Shopify's response
//return array('headers' => $headers, 'response' => $response[1]);changed headers
return array('headers' => $header_data, 'response' => $response[1]);
}
}
Trying to create a php script which will receive JSON from Dialogflow webhook.
The data then needs to be parsed and
<?php
$input = json_decode(file_get_contents('php://input'), true);
$messageId = $input["resolvedQuery"];
$text = print_r($input,true);
file_put_contents('output.txt', var_export($text, TRUE));
$url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key=APAue83jLrt7xFeQoGjgKq&message=" . $messageId;
$data = array("result" => "resolvedQuery");
$ch = curl_init($url);
$data_string = json_encode($messageId);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("resolvedQuery"=>$data_string));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type:application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
?>
The data i need is from resolvedQuery but im not getting any output.
Would someone point me in the right direction please
So from my understanding, you want resolvedQuery from api.ai in your webhook. All you need to do is change:
$messageId = $input["resolvedQuery"];
to
$messageId = $input['result']['resolvedQuery'];
Check and tell me if it's working. I did skype, fb, and google integrations in php so if you need any help feel free to ask.
Does anyone know if its possible to create a Facebook notification either via Vb.Net or PHP using the Facebook SDK version 4.
Ideally I'd like to have users on my website enter their Facebook login details which I'd then store their [facebook id] so I could send a weekly status notification via VB.Net to their Facebook account.
Edited
So far I get the scope ID from javascript and for testing copy it to PHP
<?php
$appId = '77585851581xxxx';
$appSecret = 'xxxx';
$userId = '1015273548332xxxx'; //Scope ID
$accesstoken = $appId . '|' . $appSecret;
$notificationText = 'The cake is a lie.';
//Get Access Token
$args = array('grant_type' => 'client_credentials',
'client_id' => $appId,
'client_secret' => $appSecret);
$ch = curl_init();
$url = 'https://graph.facebook.com/oauth/access_token';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$returnValue = curl_exec($ch);
curl_close($ch);
echo $returnValue;
$accesstoken = $returnValue;
//Set Notification
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook');
$url = 'https://graph.facebook.com/' . $userId . '/notifications' .
'?access_token=' . $accesstoken .
'&template=' . $notificationText .
'&href=';
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
echo $curlResult;
?>
I now get the error:
Sorry, something went wrong. We're working on getting this fixed as soon as we can.
Tutorial for the PHP SDK 4.0: http://www.devils-heaven.com/facebook-php-sdk-4-0-tutorial/
Although i would suggest using simple CURL calls for App Notifications, you don´t need the PHP SDK for that. Example code can be found here: http://blog.limesoda.com/2014/08/app-notifications-facebook-apps/
The article is in german, but the code is the relevant part:
$accessToken = $appId . '|' . $appSecret;
$notificationText = 'The cake is a lie.';
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook');
//send notification
$url = 'https://graph.facebook.com/' . $userId . '/notifications' .
'?access_token=' . $accesstoken .
'&template=' . $notificationText .
'&href=';
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
I am trying to send some custom data with the payload to iOS devices. I use the following code to successfully send pushes to all devices but I need to send some additional data. I couldn't find any documentation for urban airship and everything I tried just gives me errors.
Code:
$contents = array();
$contents['badge'] = "+1";
$contents['alert'] = $message;
$contents['sound'] = "cat.caf";
$notification = array();
$notification['ios'] = $contents;
$platform = array();
array_push($platform, "ios");
$push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform);
$json = json_encode($push);
$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);
After some research I came to this. In the 'contents' section of your code add this line:
$contents['extra'] = array("message"=>"another message");
This should enable you to send additional custom data along with your push.
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);
?>