message attachment sent by office365 rest api is not shown in inbox - office365api

I am using office 365 rest api and sending attachment, when I am sending attachment to any email address other than office 365 mail like(gmail.com, yahoo.com etc) then attachment seen in inbox, but in case of office 365 mail it showing only content.
Thanks,
Shankar

I am able to send attachment to both office 365 and yahoo, gmail etc by using rest api of office 365.
Attachment array need to created as follows
<?php
$k=0;
$attchmentArr=array();
foreach($requestjson['name'] as $file){
$attchmentArr[]=array(
"#odata.type"=> "#Microsoft.OutlookServices.FileAttachment",
"Name"=> $file,
"ContentBytes"=> $requestjson['content_bytes'][$k],
);
$k++;
}
?>
You need to create an array like following.
<?php
$post_data=array(
"Message"=>array(
"Subject"=>$requestjson['subject'],
"Body"=>array(
'ContentType'=>'HTML',
'Content'=> $requestjson['message'],
),
"From"=>array(
"EmailAddress"=>array(
'Name'=>$fromName[0],
'Address'=>CARE_ACOCUNT
)
),
"ToRecipients"=>array(
array(
"EmailAddress"=>array(
'Name'=>$toName[0],
"Address"=>$requestjson['to_address']
)
)
),
"Attachments"=>$attchmentArr,
),
"SaveToSentItems"=> "true"
);
?>
then use curl to send mail
<?php
$url="https://outlook.office365.com/api/beta/me/sendmail";
$post_data = json_encode($post_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, CARE_ACOCUNT . ':' . CARE_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $post_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response = json_decode($response, true);
$status_arr=array(200,201,202,204);
curl_close($ch);
if(in_array($status,$status_arr)){
return $response;
} else {
return 'error';
}

Related

Hubspot Webhooks: Redirect Auth & BAD_GRANT_TYPE Message

I'm trying to add a custom app in Hubspot. I'm using PHP. I have:
A Hubspot app
A working webhook URL
A redirect URL with the following code
$url = 'https://api.hubapi.com/oauth/v1/token';
$code = $_GET['code']; // Getting code parameter from redirect URL
//echo $code;
$fields = array();
$fields['grant_type'] = 'authorization_code'; // Have also tried 'refresh_token' - same result
$fields['client_id'] = '{CLIENT_ID}';
$fields['client_secret'] = '{CLIENT_SECRET}';
$fields['redirect_uri'] = '{REDIRECT_URI}';
$fields['code'] = trim($code);
//$fields['refresh_token'] = trim($code); // Have also tried 'refresh_token' - same result
$request_type = 'POST';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', $headers));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
if($fields){
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close($ch);
echo var_dump ($return);
My understanding is that Hubspot will use the 'code' parameter passed to the on the "authorization" URL to validate the authorization. However, I keep getting this error:
string(171) "{"status":"BAD_GRANT_TYPE","message":"missing or unknown grant type","correlationId":"f0508752-24bc-40e4-b0bc-51e358459653","requestId":"96044613d4e4a5aab7356fab04001e5c"}"
I had the same error response and it seemed that this is an encoding issue.
I debugged with Postman and found out that the following code works:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.hubapi.com/oauth/v1/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=authorization_code&code=string&redirect_uri=string&client_id=string&client_secret=string",
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded",
"Cookie: __cfduid=someidfrompostman"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

Sending Firebase push notifications from Laravel

I am writing an iOS application, using laravel for API and google firebase for push notifications. When I send a push notification using firebase cloud messaging, it comes to my device. When I send push notifications using laravel, it does not affect. Here is my script for sending push notifications from laravel:
function sendNotification(Request $request)
{
$friendToken = [];
$usernames = $request->all()['friend_usernames'];
$dialog_id = $request->all()['dialog_id'];
foreach ($usernames as $username) {
$friendToken[] = DB::table('users')->where('user_name', $username)
->get()->pluck('device_token')[0];
}
$url = 'https://fcm.googleapis.com/fcm/send';
foreach ($friendToken as $tok) {
$fields = array(
'to' => $tok,
'data' => $message = array(
"message" => $request->all()['message'],
"dialog_id" => $dialog_id
)
);
$headers = array(
'Authorization: key=*mykey*',
'Content-type: Application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_exec($ch);
curl_close($ch);
}
$res = ['error' => null, 'result' => "friends invited"];
return $res;
}
It returns a successful result, but notification not sent to iOS devices.
PS: It works successfully on Android devices.
After some research I have found a solution to my question. In order to work for iOS platform, we need to add notification in fields:
$notification = array('title' =>"" , 'text' => $request->all()['message']);
$fields = array(
'to' => $tok,
'data' => $message = array(
"message" => $request->all()['message'],
"dialog_id" => $dialog_id
),
'notification' => $notification
);

iOS Swift - Is it possible to send notifications using FCM directly from PHP?

I'm sending push notifications using FCM to Android with MySSQL and PHP. This is my code:
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = YOUR_KEY ',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$conn = mysqli_connect("localhost","root","","fcm");
$sql = " Select Token From users";
$result = mysqli_query($conn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token"];
}
}
mysqli_close($conn);
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
But I tried sending notifications on iOS(Swift) devices and it does not work. I get the token (FIRInstanceID.instanceID().token()) and try to send a notification from send.php (above code) and not worked.
I send individual/group/topic notification from Firebase Console and worked perfectly. Why doesn't it work from file.php?
help?!
To send iOS through FCM, you need to define notification key in your payload. It is explained in docs please take a look at there. https://firebase.google.com/docs/cloud-messaging/ios/send-multiple
Here is an example request
{
"condition": "'all' in topics",
"priority" : "high",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message Title",
}
}
In the example above I'm sending to the topic named all. You should create topic and subscribe your users to related topics.
You need to make necessary changes in your $fields variable.

Artdarek oauth-4-laravel / Lusitanian/PHPoAuthLib: Posting LinkedIn Share API not working

I am using https://github.com/artdarek/oauth-4-laravel for my latest project. I was able to implement LinkedIn OAuth using this, but the posting share is not working. Here is my code:
$params['content'] ='
<share>
<comment>Check out the LinkedIn Share API!</comment>
<content>
<title>LinkedIn Developers Documentation On Using the Share API</title><description>Leverage the Share API to maximize engagement on user-generated content on LinkedIn</description>
<submitted-url>https://developer.linkedin.com/documents/share-api</submitted-url>
<submitted-image-url>http://m3.licdn.com/media/p/3/000/124/1a6/089a29a.png</submitted-image-url>
</content>
<visibility>
<code>connections-only</code>
</visibility>
</share>';
$extraHeaders = array(
'Content-type' => 'application/xml',
);
$status = json_decode($linkedinService->request('people/~/shares?format=json', 'POST',
$params, $extraHeaders ));
But the posting is not working. But I can post using cURL, so I think my access_token is OK. Here is my cURL code
$handle = curl_init();
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_URL, "https://api.linkedin.com/v1/groups/{$post->linkedInGroupAccount->linkedin_id}/posts?format=json&oauth2_access_token={$post->linkedInGroupAccount->access_token}");
curl_setopt($handle, CURLOPT_VERBOSE, TRUE);
$header[] = 'Content-Type: text/xml; charset=UTF-8';
curl_setopt($handle, CURLOPT_POSTFIELDS, $content);
curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
$post->msg = curl_exec($handle);
curl_close($handle)
Why is the $linkedinService->request() not working?
try with this code
you xml content should like this
$XML = "<share>
<comment>This is a comment</comment>
<content>
<title>This is the title</title>
<submitted-url>http://yourapp.dev</submitted-url>
<submitted-image-url>http://yourapp.dev/image.jpg</submitted-image-url>
<description>#riseofthetiger</description>
</content>
<visibility><code>anyone</code></visibility>
</share>";
and your header should like this
$headers = array(
'Content-Type' => 'application/xml',
'x-li-format' => 'xml',
);
then request like this
$result = json_decode( $linkedin->request('/people/~/shares?format=json', 'POST', $XML, $headers),true );
hope that worked

How I can get account id and clientFolderId in icontact API

My question is specific for iContact API. I have register an application and get API id. But I am not able to find accountId and clientFolderId.
Please see this below link :
http://developer.icontact.com/documentation/request-your-accountid-and-clientfolderid/
At above page "Perform a GET on the Accounts resource" How I can perform this to get account id and clientfolderid.
The easiest way I have found: Login to sandbox or to your real iContact account, in the main menu go to Contact -> Sign-up forms, then create just any form, click on view HTML and you will find Account Id there.
This is my full code to get the account id and client folder id, thanks to Carlos Duran above for getting some of my code problems worked out:
/* iContact LIVE * /
$GLOBALS['iContact_settings'] = array(
'apiUrl' => 'https://app.icontact.com',
'apiPage' => '/icp/a/',
'username' => 'username',
'password' => 'password',
'appId' => 'appId'
);
/* iContact SANDBOX */
$GLOBALS['iContact_settings'] = array(
'apiUrl' => 'https://app.sandbox.icontact.com',
'apiPage' => '/icp/a/',
'username' => 'username-beta',
'password' => 'password',
'appId' => 'appId'
);
/**/
$icontact_url = $GLOBALS['iContact_settings']['apiUrl'] . $GLOBALS['iContact_settings']['apiPage'];
$icontact_page = $GLOBALS['iContact_settings']['apiPage'];
$icontact_headers = array(
"GET ".$icontact_page." HTTP/1.0",
"Accept: text/xml",
"Content-Type: text/xml",
"API-Version: 2.2",
"API-AppId: " . $GLOBALS['iContact_settings']['appId'],
"API-Username: " . $GLOBALS['iContact_settings']['username'],
"API-Password: " . $GLOBALS['iContact_settings']['password']
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $icontact_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $icontact_headers);
$data = curl_exec($ch);
curl_close($ch);
$account_id = "";
if (($pos=strpos($data,"<accountId>"))!==false){
$account_id = substr($data, strlen("<accountId>")+$pos);
if (($pos=strpos($account_id,"<"))!==false){
$account_id = substr($account_id, 0, $pos);
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $icontact_url ."$account_id/c/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $icontact_headers);
$data = curl_exec($ch);
curl_close($ch);
$client_folder_id = "";
if (($pos=strpos($data,"<clientFolderId>"))!==false){
$client_folder_id = substr($data, strlen("<clientFolderId>")+$pos);
if (($pos=strpos($client_folder_id,"<"))!==false){
$client_folder_id = substr($client_folder_id, 0, $pos);
}
}
I just switched to JSON, way better.
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $icontact_url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, $icontact_headers);
$data = curl_exec($handle);
curl_close($handle);
$decoded = json_decode($data);
$account_id = $decoded->accounts[0]->accountId;
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $icontact_url ."$account_id/c/");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, $icontact_headers);
$data = curl_exec($handle);
curl_close($handle);
$decoded = json_decode($data);
$client_folder_
id = $decoded->clientfolders[0]->clientFolderId;
And use:
"Accept: application/json",
"Content-Type: application/json",
Instead of text/xml above.
The only way to work with the iContact API, is to send correct headers to the server, then you will be able to make any of the requests and actions that appear in the documentation.
The best way that I found to do this is by setting up a PHP script with cUrl
$url = "https://app.sandbox.icontact.com/icp/a/";
$page = "/icp/a/";
$headers = array(
"GET ".$page." HTTP/1.0",
"Accept: text/html",
"Content-Type: text/html",
"API-Version: 2.2",
"API-AppId: yourapiappid",
"API-Username: yourapiusername",
"API-Password: yourappidpassword"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
Here you get your accountId and the rest is just calling the right url with this script!
Hope it gives you a hint.
"Keep up the good coding."
This PHP iContact API is rather useful https://github.com/icontact

Resources