I am using following PHP script to send OneSignal push notifications to subscribed devices.
<?PHP
function sendMessage(){
$content = array(
"en" => 'Testing Message desde el backend small icon '
);
$fields = array(
'app_id' => "xxxx",
'filters' => array(array("field" => "tag", "key" => "correo", "relation" => "=", "value" => "xxx")),
'data' => array("foo" => "bar"),
'small_icon' =>"ic_push",
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic xxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");
?>
This script is working fine, and the notifications are sent.
Now I need to add more filters, for example I need to add a filter to two other segments that I have created at the OneSignal dashboard.
The segments are "skateboard" and "administrators".
How can I add these two segment to the filters array?
You need to pass segments name array in fields like you are passing tags.
$content = array(
"en" => 'Notification Message Here..'
);
$heading = array(
"en" => 'Heading goes here..'
);
$fields = array(
'app_id' => 'XXXXXXXXXXXXXXXXXXXXX',
'contents' => $content,
'headings' => $heading,
'included_segments' => array('SegmentName1','SegmentName2'),
'tags' = array(array("key" => "state", "relation" => "=", "value" => 'Delhi'))
);
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic XXXXXXXXXXXXXXXXXXX'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
I am trying to send user-specific web notifications with onesignal and i solved everything except this variable null problem.
I use that variable multiple times and there is no problem except these lines:
<?php
if(isset($_POST["sub"]))
{
$my_variable= $_POST["t1"];
$SQL = "some sql here";
if (mysqli_query($db, $SQL)) {
echo $my_variable;
echo "<br>";
function sendMessage(){
$content = array(
"en" => 'test message'
);
$fields = array(
'app_id' => "5b0eacfc-3ac8-4dc6-891b-xxxxx",
'filters' => array(array("field" => "tag", "key" => "key", "relation" => "=", "value" => "$my_variable")),
'data' => array("foo" => "bar"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic xxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
The result is :
1 JSON sent: {"app_id":"5b0eacfc-3ac8-4dc6-891b-xxxxx","filters":[{"field":"tag","key":"key","relation":"=","value":"null"}],"data":{"foo":"bar"},"contents":{"en":"test message"}}
I tried so many variations with/without quotas , json_encode() function but couldn't pass that variable to that array.
Your variable is out of scope.
You define $my_variable outside of the function sendMessage(), but proceed to try and use it within the function, without passing it as a parameter.
This can be fixed with the following:
function sendMessage($filterValue)
{
$content = array(
"en" => 'test message'
);
$fields = array(
'app_id' => "5b0eacfc-3ac8-4dc6-891b-xxxxx",
'filters' => array(array("field" => "tag", "key" => "key", "relation" => "=", "value" => $filterValue)),
'data' => array("foo" => "bar"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic xxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage($my_variable);
$return["allresponses"] = $response;
$return = json_encode( $return);
I am trying to send a mail with attachments with mailgun.
The mail itself is fine, but it is missing the attachment.
Also in the mailgun log it shows up fine, but the attachment array is empty.
I replaced my credentials with example.com.
The file is placed in a subdirectory and is readable.
$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example#mail.example.com>',
'to' => 'example#example.com',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => '#' . $filename);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
I don't get an error, this is the $response:
string(103) "{
"id": "<20170514122638.8820.55203.4543B111#mail.example.com>",
"message": "Queued. Thank you."
}"
Within the mailgun logs no attachments are listed:
"message": {
"headers": {
"to": "example#example.com",
"message-id": "20170514122638.8820.55203.4543B111#mail.example.com",
"from": "Example <example#mail.example.com>",
"subject": "Subject"
},
"attachments": [],
"size": 349
},
According to all documentation I found this would be the correct solution, but it is not working.
Thanks in advance for all replies.
Change your first code to:
$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example#mail.example.com>',
'to' => 'example#example.com',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => curl_file_create($filename, 'application/pdf', 'example.pdf'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
I changed '#'.$filename to curl_file_create($filename, 'application/pdf', 'example.pdf').
See documentation curl_file_create and check the notes for PHP < 5.5.
or use the api https://documentation.mailgun.com/en/latest/api-sending.html#examples
once the domains and parameters are set you can then just use $result = $mgClient->messages()->send( $domain, $params );
This worked for me.
<?php
$path = $filename;
define('MAILGUN_URL', 'https://api.mailgun.net/v3/domainname');
define('MAILGUN_KEY', 'private api key from mail gun');
**function sendmailbymailgun**($to,$toname,$mailfromname,$mailfrom,$subject,
$html,$text,$tag,$replyto, $path){
$array_data = array(
'from'=> $mailfromname .'<'.$mailfrom.'>',
'to'=>$toname.'<'.$to.'>',
'subject'=>$subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyto,
'attachment[0]' => curl_file_create(__dir__."\\".$path, 'application/pdf', $path),
'attachment[1]' => curl_file_create(__dir__."\\".$path, 'application/pdf', "example.pdf")
);
$session = curl_init(MAILGUN_URL.'/messages');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, 'api:'.MAILGUN_KEY);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $array_data);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
$results = json_decode($response, true);
return $results;
}
//: call the function
$res = sendmailbymailgun("example#yahoo.com","Recipeint Name", "Sender Name", "sender#example.com","Email subject","Email body. find two attachment","","tags", "no-reply#example.com", $path);
echo "<pre>".print_r($res, true)."</pre>";
?>
$dest = "filepath/test.pdf";
$result = $mgClient->messages()->send($domain,
array('from' => 'Tester <test#test.com>',
'to' => 'test#test02.com',
'subject' => 'Testing',
'html' => '<h1>Normal Testing</h1>',
'attachment' => array(
array(
'filePath' => $dest,
'filename' => 'test.pdf'
)
)
));
This code is worked properly for me.
This works for me. I have passed the attachment file URL in the attachment array. now I can send multiple attachments in email.
$attachment = [ 0 =>
'https://www.crm.truerater.com/public/assets/client_upload_images/1634327873.png',
1 => 'https://www.crm.truerater.com/public/assets/client_upload_images/1634327873.png'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/truerater.com/messages');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'from' => $mailfromname .'<'.$mailfrom.'>',
'to' => $toname.'<'.$to.'>',
'cc' => '',
'bcc' => '',
'subject' => $subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyto,
);
if(sizeOf($attachment) > 0){
$i=0;
foreach ($attachment as $attach){
$attachPath = substr($attach, strrpos($attach, '/public/') + 1);
$post['attachment['.$i.']'] = curl_file_create($attach, '', substr($attachPath, strrpos($attachPath, '/') + 1));
$i=$i+1;
}
}
$headers_arr = array("Content-Type:multipart/form-data");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, 'api' . ':' . $key);
curl_setopt($ch, CURLOPT_HEADER, $headers_arr);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = curl_error($ch);
\Log::info($result);
}
else{
$result = json_decode($result,true);
}
curl_close($ch);
\Log::info($result);
return $result;
I am using the following code to insert a post in blogger but the response I am receiving from Google is null. What am I doing wrong?
<?php
$key = "MyKey";
$blog_id = "123456";
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blog_id.'/posts/';
$postData = array(
'kind' => 'blogger#post',
'blog' => array('id' => $blog_id),
'title' => 'This is title',
'content' => 'This is content'
);
$data_string = json_encode($postData);
$head = array();
$head[] = 'Authorization: '.$key;
$head[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$rsp = curl_exec($ch);
$results = json_decode($rsp);
var_dump($results);
?>
My Facebook app publishes a story to the user wall with an http post:
$args = array('access_token' => $ACCESS_TOKEN,
'message' => 'testing message',
'picture' => $appin_logo,
'link' => $appin_canvas_url,
'name' => $appin_name,
'caption' => $post_score,
'description' => $post_rfs,
);
$ch = curl_init(); $url = 'https://graph.facebook.com/me/feed'; 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); $data = curl_exec($ch); curl_close($ch);
That works all fine, except for one thing: $post_rfs is an array. I'd like to output its values in a neat way, with a comma after every value i.e.. What should I do?
Thanks in advance.
Try this:
implode(', ', array_values($post_rfs));