I am trying to send TOAST (PUSH) notification to Windows Phone Application (8.1) from PHP application. Configuration for the notification is done right . Configuration is validated with (http://31daysofwindows8.com/push) and it works fine. However when I use following code, I get notification as a string "New Notification". This notification does not have header, default image as it should be .Also what we observed is when XML payload is commented and a simple string is send, the same notification is receieved. I doubt the XML payload what is send is not right. Please guide me
$tokenRequest = curl_init();
curl_setopt($tokenRequest, CURLOPT_URL, 'https://login.live.com/accesstoken.srf');
curl_setopt($tokenRequest, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
//FIELDS
$fields = array(
'grant_type' => 'client_credentials',
'client_id' => 'our client id',
'client_secret' => 'our client secret',
'scope' => 'notify.windows.com'
);
$fields_string = "";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
curl_setopt($tokenRequest, CURLOPT_RETURNTRANSFER, true);
curl_setopt($tokenRequest,CURLOPT_POST, count($fields));
curl_setopt($tokenRequest,CURLOPT_POSTFIELDS, $fields_string);
$output = json_decode(curl_exec($tokenRequest));
curl_close($tokenRequest);
echo "<br>";
echo "<br>";
$accessToken = $output->{'access_token'};
$sendPush = curl_init();
curl_setopt($sendPush, CURLOPT_URL, 'our URI here');
//TOAST MESSAGE
$toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
"<wp:Notification xmlns:wp=\"WPNotification\">" .
"<wp:Toast>" .
"<wp:Text1>Text...</wp:Text1>" .
"<wp:Text2>text..</wp:Text2>" .
"</wp:Toast>" .
"</wp:Notification>";
curl_setopt($sendPush, CURLOPT_HEADER, true);
echo $toastMessage;
$headers = array('Content-Type: text/xml',,"Content-Type: text/xml", "X-WNS-Type: wns/toast","Content-Length: " . strlen($toastMessage),"X-NotificationClass:2" ,"X-WindowsPhone-Target: toast","Authorization: Bearer $accessToken");
curl_setopt($sendPush, CURLOPT_HTTPHEADER, $headers);
curl_setopt($sendPush, CURLOPT_RETURNTRANSFER, true);
curl_setopt($sendPush,CURLOPT_POST, true);
curl_setopt($sendPush, CURLOPT_POSTFIELDS, $toastMessage));
$output = curl_exec($sendPush);
$info = curl_getinfo($sendPush);
echo($info['request_header']);
echo "<br>";
//var_dump(curl_getinfo($sendPush, CURLINFO_HTTP_CODE));
echo "<br>";
//var_dump(curl_getinfo($sendPush, CURLINFO_HEADER_OUT));
echo "<br>";
curl_close($sendPush);
The resultant XML payload is as following
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<wp:Notification
xmlns:wp=\"WPNotification\">
<wp:Toast>
<wp:Text1>Sharvin</wp:Text1>
<wp:Text2>Notif</wp:Text2>
</wp:Toast>
</wp:Notification>"
Try to change your toastMessage to :
$toastMessage = "<toast>".
"<visual>".
"<binding template=\"ToastText01\">".
"<text id=\"1\">".$message."</text>".
"</binding>".
"</visual>".
"</toast>";
You can find all the toast catalogue here https://msdn.microsoft.com/en-us/library/windows/apps/hh761494.aspx?f=255&MSPPError=-2147217396
Try to use below code, This code is well tested and working in many apps.
public function sendPushNotification($notify_url, $msg, $type) {
$delay = 1;
$msg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
"<wp:Notification xmlns:wp=\"WPNotification\">" .
"<wp:Toast>" .
"<wp:typessss>".$type."</wp:typessss>" .
"<wp:Datassss>".$msg."</wp:Datassss>" .
"</wp:Toast>" .
"</wp:Notification>";
$sendedheaders = array(
'Content-Type: text/xml',
'Accept: application/*',
'X-WindowsPhone-Target: toast',
"X-NotificationClass: $delay"
);
$req = curl_init();
curl_setopt($req, CURLOPT_HEADER, true);
curl_setopt($req, CURLOPT_HTTPHEADER,$sendedheaders);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $msg);
curl_setopt($req, CURLOPT_URL, $notify_url);
curl_setopt($req, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($req);
//echo '<pre>';
//print_r($response); die;
curl_close($req);
}
Related
I'm trying to POST an image on Mastodon (specifically on Humblr) but I cannot get the media_id, the response is null, but I'm not sure where the problem is.
I can publish text, no problem, so the authentication part is fine, I only have the problem with the image, the only difference I can see in the documentation is that "Media file encoded using multipart/form-data".
Here's my code so far...
$headers = ['Authorization: Bearer '.$settings['access_token'] , 'Content-Type: multipart/form-data'];
$mime_type = mime_content_type($urlImage);
$cf = curl_file_create($urlImage,$mime_type,'file');
$media_data = array( "file" => $cf);
$ch_status = curl_init();
curl_setopt($ch_status, CURLOPT_URL, "https://humblr.social/api/v1/media");
curl_setopt($ch_status, CURLOPT_POST, 1);
curl_setopt($ch_status, CURLOPT_POSTFIELDS, $media_data);
curl_setopt($ch_status, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_status, CURLOPT_HTTPHEADER, $headers);
$media_status = json_decode(curl_exec($ch_status));
echo "Response: ".json_encode($media_status);
From this I want to extract the $media_status-> media_id
I don't really know much about 'multipart/form-data' to be honest.
Am I missing something?
This took some trial and error for me as well. Here's what worked for me (PHP 7.3):
$curl_file = curl_file_create('/path/to/file.jpg','image/jpg','file.jpg');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://instanceurl.com/api/v1/media');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer YOUR_TOKEN_HERE',
'Content-Type: multipart/form-data'
]);
$body = [
'file' => $curl_file
];
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
if (!$response) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;
curl_close($ch);
I am currently trying to post a message on my Discord channel trying to use a cURL POST type. The issue that I am getting when I run my code is that it is giving me a 401 error saying I'm unauthorized. I am running my PHP code on a webserver using xampp localhost. I also went in and tried to authorize my application bot via URL link (https://discordapp.com/oauth2/authorize?client_id=MYAPPLICATIONID&scope=bot&permissions=8) and have successfully added the bot into my channel. Have a look at my code
$data = array("Authorization: Bot" => $clientSecret, 'content' => 'Test Message');
$data_string = json_encode($data);
$ch = curl_init('https://discordapp.com/api/v6/channels/'.$myChannel.'/messages');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$answer = curl_exec($ch);
echo $answer;
if (curl_error($ch)) {
echo curl_error($ch);
}
I get $clientSecret from the application page to reveal my client secret token and $myChannel is my discords channel/server id.
NOTE: I have modeled my code off another stackoverflow answer given here discord php curl login Fail . So I am unsure if I am using the correct syntax for the an application bot
Here is full code (Without cURL). Just replace the string WEBHOOK_HERE with your bot's webhook:
<?php
$message = $_POST['message'];
$data = ['content' => $message];
$options = [
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => json_encode($data)
]
];
$context = stream_context_create($options);
$result = file_get_contents('WEBHOOK_HERE', false, $context);
?>
<form method="post">
Type your message here :<br><input type="text" name="message"><br>
<input type="submit" value="Submit">
</form>
I'm new here so I hope you enjoy the code!
Close! Your solution has an error.
<?php
$myChannel = "CHANNEL_ID";
$myToken = "TOKEN";
$msg = 'Test Message';
$data = array('content' => $msg);
$data_string = json_encode($data);
$ch = curl_init('https://discordapp.com/api/v6/channels/' . $myChannel . '/messages');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: Bot ' . $myToken
)
);
$answer = curl_exec($ch);
echo $answer;
if (curl_error($ch)) {
echo curl_error($ch);
}
?>
I just tested this on my server and it worked. with Bot ' . $token it failed.
I actually had a tough time searching the web for this, without using webhooks. I wanted to post the message as a bot, not a webhook. The error was with your authorization, as it needs to be in the header... So I'm posting the correct solution here for future googlers.
<?php
$myChannel = "CHANNEL_ID";
$myToken = "TOKEN";
$msg = 'Test Message';
$data = array('content' => $msg);
$data_string = json_encode($data);
$ch = curl_init('https://discord.com/api/v6/channels/' . $myChannel . '/messages');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: Bot ' . $myToken
)
);
$answer = curl_exec($ch);
echo $answer;
if (curl_error($ch)) {
echo curl_error($ch);
}
?>
I have a sand box account in fortnox and i am trying to get the access token using following code, but i keep getting same error:
$requestMethod = "GET";
$ch = curl_init();
$options = array(
'Authorization-Code: '. AUTHORIZATION_CODE .'',
'Client-Secret: '. CLIENT_SECRET .'',
'Content-Type: '. CONTENT_TYPE .'',
'Accept: '. ACCEPTS .''
);
curl_setopt ($ch, CURLOPT_CAINFO, "/xampp/htdocs/cacert.pem");
curl_setopt($ch, CURLOPT_URL, "https://api.fortnox.se/3/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, $options);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requestMethod);
$curlResponse = curl_exec($ch);
$info = curl_getinfo($ch);
//echo 'Took ' . $info['total_time'] . ' seconds for url ' . $info['url'];
echo $curlResponse;
if ($curlResponse === FALSE) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
Error: PHP / 4.5.37 Can not sign in , access tokens , or client- secret missing ( 2000311 ) .
I get the Access-Token using the following code which is very close to your code. It worked fine for me.
Your error is described on the Errors page, but it adds nothing new to the text you already shown.
Please note that you can retrive the Access-Token only once, check Authentication section in the Fortnox documentation.
An Access-Token can only be retrieved once with every Authorization-Code, multiple requests with the same Authorization-Code will make both the Authorization-Code and the Access-Token invalid.
public function actionRetrieveAccessToken($authCode, $clientSecret)
{
$headers = [
'Authorization-Code: ' . $authCode,
'Client-Secret: ' . $clientSecret,
'Content-Type: application/json',
'Accept: application/json',
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.fortnox.se/3/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$responseText = curl_exec($curl);
$response = json_decode($responseText, true);
if (isset($response['ErrorInformation'])) {
echo 'Request failed with error messages:' . PHP_EOL;
echo $response['ErrorInformation']['Error'] . ': ';
echo $response['ErrorInformation']['Message'];
echo ' (' . $response['ErrorInformation']['Code'] . ')' . PHP_EOL;
} else {
echo 'Access Token: ' . $response['Authorization']['AccessToken'] . PHP_EOL;
}
}
$content_type = 'application/json';
$api_url="api url here";
$curl = curl_init();
$headers = array(
'Content-Type: '.$content_type.'',
'Authorization : '.'Bearer ' . $this->access_token
);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($data))
{
$data = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($curl);
logthis($response,"utils");
curl_close($curl);
I am using ParseAPI (https://parse.com/docs/rest/guide#objects-updating-objects) and I'm attempting to update an object with their rest api. I get a response back from parse saying the object was updated, however no change was made.
Can someone please guide me in the correct direction?
Thank you!!
<?php
$url = ' https://api.parse.com/1/classes/GameScore/Ed1nuqPvcm';
$appId = 'my app id';
$restKey = 'my rest key';
$headers = array(
"Content-Type: application/json",
"X-Parse-REST-API-Key: " . $restKey,
"X-Parse-Application-Id: " . $appId
);
$data = array('pass' => 'hi');
$jsonDataEncoded = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($jsonDataEncoded));
$response = curl_exec($ch);
echo $response;
?>
I have looked over the code many times but whenever I send request to API it returns "message":"invalid signature"
I am thinking it has to do with hashing the body, possibly. I'm new to PHP and its my first project. :)
Anyone see an error? Thanks.
<?php
$arr = array('size' => ".01", 'price' => '240', 'side' => 'sell',
'product_id' => 'BTC-USD');
$output = json_encode($arr);
echo json_encode($arr)."<br/>";
$key = "f23612b06cb4d020cda7e04b1ae6ef9a";
$secret = "RENqodtuTCn4v7g7Pn/FFdQAIKReVXGayNPrNN/Zb7AjATI0hP4R0MCDD5RqnDu60qTZ5Qry329fFu7kcObGBw==";
$passphrase = "tradebot";
$time = time();
$url = "https://api.gdax.com/orders";
$data = $time."POST"."/orders";
echo $data . "<br/>";
$hashinput = "$output"."$data";
$sign = base64_encode(hash_hmac("sha256", $hashinput, base64_decode($secret), true));
echo $sign . "<br/>";
$headers = array(
'CB-ACCESS-KEY: '.$key,
'CB-ACCESS-SIGN: '.$sign,
'CB-ACCESS-TIMESTAMP: '.$time,
'CB-ACCESS-PASSPHRASE: '.$passphrase,
'Content-Type: application/json'
);
var_dump($headers);
echo $url;
static $ch = null;
if (is_null($ch)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'local server');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, $output);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$res = curl_exec($ch);
echo $res;
}
replace
$hashinput = "$output"."$data";
with
$hashinput = "$data"."$output";
and replace
curl_setopt($ch, CURLOPT_POST, $output);
with
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $output);
(from Coinbase Community forum)