Send image as attachment as well as inline using Sendgrid API - php

I want to send an email with the image as an attachment and also embed it in the body.
<?php
$email = "to#example.com";
$name = "some_name";
$img = file_get_contents('image.jpg');
$body = '<img src = "cid:image">';
$subject = "Test email";
$headers = array(
'Authorization: Bearer API_KEY',
'Content-Type: application/json'
);
$data = array(
"personalizations" => array(
array(
"to" => array(
array(
"email" => $email,
"name" => $name
)
)
)
),
"from" => array(
"email" => "from#example.com"
),
"subject" => $subject,
"content" => array(
array(
"type" => "text/html",
"value" => $body
)
),
"attachments" => array(
array(
"content" => base64_encode($img),
"type" => "image/jpeg",
"filename" => "image",
"disposition" => "inline",
"content_ID" => "image",
//"disposition" => "attachment"
)
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sendgrid.com/v3/mail/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;?>
The problem is that it is either going as inline content or an attachment but not both. I don't know how to solve this.
I tried adding two separate attachments blocks for inline and attachment, but it's always considering the second block.
Nothing seems to work.

Twilio SendGrid developer evangelist here.
You don't want to set two of the same keys in an array (as you are doing for disposition) or set two attachments blocks. Instead, you should add two items to the attachments block, like this:
"attachments" => array(
array(
"content" => base64_encode($img),
"type" => "image/jpeg",
"filename" => "image-inline",
"disposition" => "inline",
"content_ID" => "image-inline",
),
array(
"content" => base64_encode($img),
"type" => "image/jpeg",
"filename" => "image-attachment",
"disposition" => "attachment",
"content_ID" => "image-attachment",
),
)
Notice I also provided different filenames and content IDs for the inline and attached files.

Related

Telegram Bot Callback-Query not sent

I've created 2 Inline-Button and I want to evaluate the callback_data. But unfortunately, the callback_data is not sent.
<?php
$bot_token = 'There is no Token seen'; // Telegram bot token
$url = "https://api.telegram.org/bot$bot_token/sendMessage";
$content = file_get_contents('php://input');
$update = json_decode($content, TRUE);
$callback_query = $update['callbackQuery'];
$callback_data = $callback_query['data'];
$ser_update = serialize($update);
db_query("INSERT INTO prefix_telegram (text) VALUES ('".$ser_update."')");
if (isset($update['message']['text'])) {
$text = $update['message']['text'];
$chat_id = $update['message']['chat']['id'];
if (strpos($text, 'outi') !== false) {
$reply = utf8_encode("Wähle einen Button!");
$keyboard = array(
"keyboard" => array(
array(
array(
"text" => "Button1",
"callback_data" => "1",
),
array(
"text" => "Button2",
"callback_data" => "2",
),
)
),
"one_time_keyboard" => true,
"resize_keyboard" => true
);
$postfields = array(
'chat_id' => "$chat_id",
'text' => "$reply",
'reply_markup' => json_encode($keyboard)
);
if (!$curld = curl_init()) {
exit;
}
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curld, CURLOPT_URL,$url);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curld);
curl_close ($curld);
}
}
Result of file_get_contents
Could anyone help me with this problem? I have sent more information than just the text of the clicked button. Thanks!
There is muuuuch to correct.
In line 7, it is $callback_query = $update['callback_query'];
From line 19 to 34:
$keyboard = array(
"inline_keyboard" => array( // inline_keyboard not keyboard
array(
array(
"text" => "Button1",
"callback_data" => "1",
),
array(
"text" => "Button2",
"callback_data" => "2",
),
)
) //Removed onetimekeyboard etc.
);
From 36 to 40:
$postfields = array(
'chat_id' => $chat_id, // you don't have to use quotes when you're using variables or numbers.
'text' => $reply,
'reply_markup' => json_encode($keyboard)
);
The callback_data is not supported for reply_markup.keyboard. It is supported only for reply_markup.inline_keyboard.
You can vote for the issue Add callback_data to KeyboardButton (reply_markup.keyboard).

Convert nested array to json in PHP without additional coding

I want to convert my array to a Json String and use it for send message using Firebase Cloud Messaging Server.
My array is :
$data = array(
"to" => "cMbucIFcJMA:APA91bH_Wq0sPrMhTmhJ9gPJX7GUsRo...",
"notification" =>
array(
"body" => "sadeq",
"title" => "test notification"
)
);
and when I use json_encode($data) my back String is:
{"to":"cMbucIFcJMA:APA91bH_Wq0sPr.... \n","notification":"Array"}
how can I convert this array to valid Json without have Array in my notification String???
My completely function is:
public function SendFromPost(){
$data = array(
"to" => "cMbucIFcJMA:APA91bH_..... ",
"notification" =>
array(
"body" => "sadeq",
"title" => "test notification"
)
);
$ch = curl_init();
curl_setopt($ch , CURLOPT_URL , "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch , CURLOPT_POST , 1);
curl_setopt($ch , CURLOPT_POSTFIELDS , $data );
curl_setopt($ch , CURLOPT_RETURNTRANSFER , true);
$headers = [
'Content-Type: application/json \n',
'Authorization: key=AIzaS.......'
];
curl_setopt($ch , CURLOPT_HTTPHEADER , $headers);
$server_output = curl_exec($ch);
curl_close($ch);
return var_dump(json_encode($data));
}
My code is correct. but I don't know why my php do like this.
Your code works. You've got an extra ; that isn't valid after your nested array.
<?php
$data = array(
"to" => "cMbucIFcJMA:APA91bH_Wq0sPrMhTmhJ9gPJX7GUsRo...",
"notification" =>
array(
"body" => "sadeq",
"title" => "test notification"
)
);
var_dump(json_encode($data));
Yields
string(115) "{"to":"cMbucIFcJMA:APA91bH_Wq0sPrMhTmhJ9gPJX7GUsRo...","notification":{"body":"sadeq","title":"test notification"}}"

Requesting Api Call using cURL

Someone could help me ? I'm trying to do a request by the code bellow, but anything happen, any message appears. I believe my code it's right:
public function subscribe(){
$json_url = 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/';
$json_string = json_encode(array(
"MerchantOrderId"=>"2014113245231706",
"Customer" => array(
"Name" => "Comprador rec programada"
),
"Payment" => array(
"Type" => "CreditCard",
"Amount" => 1500,
"Installments" => 1,
"SoftDescriptor" => "Assinatura Fraldas"
)
));
$ch = curl_init($json_url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string
);
curl_setopt_array( $ch, $options );
$result = curl_exec($ch); // Getting jSON result string
print_r($result);
}
Find link with instructions of the site:
you will reiceive this:
[
{
"Code": 114,
"Message": "The provided MerchantId is not in correct format"
}
]
with this code:
function subscribe(){
$json_url = 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/';
$json_string = json_encode(
array(
"MerchantOrderId"=>"2014113245231706",
"Customer" => array(
"Name" => "Comprador rec programada"
),
"Payment" => array(
"Type" => "CreditCard",
"Amount" => 1500,
"Installments" => 1,
"SoftDescriptor" => "Assinatura Fraldas"
)
)
);
$headers = array(
'Content-Type: application/json',
'MerchantId: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx',
'MerchantKey: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx',
'RequestId: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx'
);
$ch = curl_init($json_url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $json_string
);
curl_setopt_array( $ch, $options ); $result = curl_exec($ch);
print_r($result);
}
subscribe()
It would be interesting what HTTP status code you get:
print_r(curl_getinfo($ch, CURLINFO_HTTP_CODE));

Confused on a SecureNet response code using cURL

I have searched Google and StackOverflow about this error, and I am having trouble finding results. Perhaps there is someone out there that can point me in the right direction, as I have never worked with the SecureNet API before.
I have a working form in PHP, however when submitting it to SecureNet, I get this as a response:
{"success":false,"result":"AUTHENTICATION_ERROR","responseCode":3,"message":"SecureNetId and SecureNetKey should be passed as Basic authentication tokens or in request object.","responseDateTime":"2015-08-27T02:58:12.54Z","rawRequest":null,"rawResponse":null,"jsonRequest":null}bool(true)
Here is my code:
$url = 'https://gwapi.demo.securenet.com/api/Payments/Charge';
$data = array(
"publickey" => $apiPkey,
"amount" => $donationAmount,
"card" => array(
"number" => $cardNumber,
"cvv" => $cvv,
"expirationDate" => $expiryMonth . '/' . $expiryYear,
"address" => array(
"line1" => $address,
"city" => $city,
"state" => $state,
"zip" => $zip
),
"firstName" => "Jack",
"lastName" => "Test"
),
"extendedInformation" => array(
"typeOfGoods" => "DIGITAL"
),
"developerApplication" => array(
"developerId" => $apiID,
"version" => $apiVersion
)
);
$secureNet = http_build_query($data);
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $secureNet);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
var_dump($result);
curl_close($ch);
I figured it out, I wasn't sending the headers.
$headers = array(
"Authorization: Basic " . base64_encode($apidID . ':' . $apiSkey)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Sending Push notification through pushwoosh from php

I am trying to send push notification through push woosh such like this :
is anybody help me how to send push notification on device from this code
function pwCall(
'createMessage', array(
'application' => PW_APPLICATION,
'auth' => PW_AUTH,
"devices" => PW_DEVICETOKEN,
'notifications' => array(
'send_date' =>'now', //gmdate('d-m-Y H:i', strtotime('2014-04-07 20:35')),
'content' => 'my custom notification testing ',
'link' => 'http://pushwoosh.com/',
'content' => array("en" => "English","ru" =>"Русский","de"=>"Deutsch")
),
'page_id' => 16863,
'link' => 'http://google.com',
'data' => array( 'custom' => 'json data' ),
)
);
I am getting error such as
Array ( [status_code] => 210 [status_message] => Cannot parse date [response] => )
notifications should be array of objects in JSON notation. In PHP it will be array of arrays. This is because you can create multiple notifications in one request.
final JSON for notifications field:
"notifications":[{ ... notification properties... }, { ... second notification properties ... }, ...]
There are only 3 root parameters in request: application(OR applications_group), auth, and notifications. Other parameters are parameters of notification, not request.
Finally your PHP call should be like following:
pwCall("createMessage", array(
"auth" => PW_AUTH,
"application" => PW_APPLICATION,
"notifications" => array(
array(
"send_date" => "now",
"content" => array("en" => "English", "ru" =>"Русский", "de"=>"Deutsch"),
"link" => "http://pushwoosh.com/",
"page_id" => 16863,
"devices" => array( PW_DEVICETOKEN ),
"data" => array( "custom" => "json data" )
)
)
));
All notification's fields except of send_date and content are optional and can be omited
By the looks of it, your date is not formatted correctly. You're passing an ordinary string consisting of the word "now". What you'll want to do is something along the lines of the following:
function pwCall("createMessage", array(
"application" => PW_APPLICATION,
"auth" => PW_AUTH,
"devices" => PW_DEVICETOKEN,
"notifications" => array(
"send_date" => gmdate("Y-m-d H:i"),
"content" => "My custom notification",
"link" => "http://pushwoosh.com/",
"content" => array("en" => "English", "ru" =>"Русский", "de"=>"Deutsch")
),
"page_i" => 16863,
"link" => "http://google.com",
"data" => array("custom" => "json data"),
)
);
we've developped an API to easily call the Pushwoosh Web Services.
This API should be a good quality one and is fully tested (very high code coverage).
https://github.com/gomoob/php-pushwoosh
$push_auth = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$push_app_id = 'XXXXX-XXXXX';
$push_debug = false;
$title = ''; // pushwoosh title
$banner = ''; // pushwoosh banner
$send_date = 'now'; // pushwoosh date
$android_header = ''; // pushwoosh android header
$android_custom_icon = '' pushwoosh notification icon;
sendpush('createMessage', array(
'application' => $push_app_id,
'auth' => $push_auth,
'notifications' => array(
array(
'send_date' => $send_date,
'content' => $title,
'android_header'=>$android_header,
'android_custom_icon' =>$android_custom_icon,
'android_badges' => 2,
'android_vibration' => 1,
'android_priority' => 1,
'data' => array('custom' => 'json data'),
),
)
));
function sendpush($method, $data) {
$url = 'https://cp.pushwoosh.com/json/1.3/' . $method;
$request = json_encode(['request' => $data]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (defined('PW_DEBUG') && self::$push_de) {
print "[PW] request: $request\n";
print "[PW] response: $response\n";
print "[PW] info: " . print_r($info, true);
}
return $info;
}
}

Categories