Adding an appointment to Exchange 2007 with php-ews - php

Does anyone have any experience with php-ews? I would like to add a new appointment to an Exchange 2007 calendar through php-ews but I'm not sure how. The documentation for php-ews is very limited. Has anyone done this before and care to provide and example?
Thanks

Ugh. I went through this a few weeks ago. Documentation for this sucks. Feel free to ask me any questions about PHP and EWS.
So assuming you mean you want to create a new Calendar event for some user's calendar, you need to begin by downloading James Armes's Exchange Web Services client: http://code.google.com/p/php-ews/source/browse/
It's a series of PHP classes which make it easy to access an Exchange server through PHP.
You then create an ExchangeWebServices object
$ews = new ExchangeWebServices(
'server address',
'username#address',
'password'
);
From there you can construct SOAP XML requests by constructing a "request" object in PHP where the attributes of the object are the layers of the SOAP request.
$request->SendMeetingInvitations = 'SendToNone';
$request->SavedItemFolderId->DistinguishedFolderId->Id = 'calendar';
$request->Items->CalendarItem->Subject = 'this is the subject of the email';
$request->Items->CalendarItem->Start = date('c', strtotime('today'));
//making this an all day event for the heck of it
$request->Items->CalendarItem->End = date('c', strtotime('today + 1 day'));
$request->Items->CalendarItem->IsAllDayEvent = true;
$request->Items->CalendarItem->LegacyFreeBusyStatus = 'Free';
$request->Items->CalendarItem->Categories->String = $category;
$request->Items->CalendarItem->Body->BodyType = 'Text';
$request->Items->CalendarItem->Body->_ = $body;
And then you send the request to the server:
$response = $ews->CreateItem($request);
var_dump-ing $response will give you the server response and give you a good idea of how the XML works.
As for what little documentation there is, the Microsoft docs will tell you how the XML requests are set up (i.e., what attributes to give which objects) and also which methods you can call on your XML requests: http://msdn.microsoft.com/en-us/library/bb204119(v=exchg.140).aspx (see "Operations" and "XML Elements")
Hope this helps! Let me know if you have any questions.

Related

Access shared mailbox through o365 v2 api

So I got access to the new o365 v2 api and it's working pretty good so far. I am however having trouble accessing any shared inboxes.
Even worse, there doesn't appear to be any error message being returned:
#odata.context = https://outlook.office.com/api/v2.0/$metadata#Me/Messages(Subject,ReceivedDateTime,SentDateTime,Sender,From,ToRecipients,CcRecipients,BccRecipients,ReplyTo,ConversationId,IsRead,InternetMessageId
[value] =
Has anyone ever tried this?
To clarify, this isn't for exchange, but outlook.com
It seems that you were using the delegate-token to request the message from the specific user for the messages in a shared box.
The Office 365 REST API only support app-level token to get the messages from the organization. The delegate-token only could get the messages of the delegatee user.
You can also consider using the EWS to retrieve the messages of shared box as a workaround.
Here is an example for your reference:
string userName = "";
string password = "";
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new NetworkCredential(userName, password);
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl(userName, RedirectionUrlValidationCallback);
FolderId SharedMailbox = new FolderId(WellKnownFolderName.Inbox, "sharedmailbox#consoto.onmicrosoft.com");
ItemView itemView = new ItemView(10);
var results = service.FindItems(SharedMailbox, itemView);
foreach (var item in results)
{
Console.WriteLine(item.Subject);
}
And if you want the Office 365 REST API to support this feature, you can also submit the feedback from here.

How to update Google Sheets file with API PHP Client

I've been taking a look at the Google API PHP Client and would like to use it to add rows to a Google Sheet. From the code, it looks like one would use this method:
public function insert($fileId, Google_Service_Drive_Property $postBody, $optParams = array())
{
$params = array('fileId' => $fileId, 'postBody' => $postBody);
$params = array_merge($params, $optParams);
return $this->call('insert', array($params), "Google_Service_Drive_Property");
}
but I can't really tell what the parameters would be. Am I heading in the right direction? Also, not quite sure on how to connect to a specific Sheet. Please advise.
Thanks!
Use Google sheets class from zend framework 1.12. They have very nicely coded library for Google Spreadsheets
https://github.com/zendframework/zf1/tree/master/library/Zend/Gdata/Spreadsheets
I figured out how to work this and wanted to share with you guys. As I stated in a comment, I did not think using Zend's GData class was a good way for me since it's very dependent on other classes throughout the framework, thus being too heavy.
So I ended up using this Spreadsheet Client on top of Google's API. Google's API is used to authenticate my service, then I start calling the Spreadsheet Client library afterwards.
After spending over a day of Googling for various problems I had for the authentication process, here's what I did to make things work:
Created a new project for Google API here
Clicked "APIs" menu on the left side under "APIs & Auth"
Searched the Drive API and enabled it (can't remember if it was necessary)
Clicked the "Credentials" menu on the left
Clicked "Create new Client ID" button under OAuth
Selected "Service Account"
After info showed & json downloaded (not needed), I clicked "Generate new P12 Key" button
I saved the p12 file somewhere I could access it through PHP
Then in the code, I added the following lines:
$email = 'somethingsomethingblahblah#developer.gserviceaccount.com';
$CLIENT_ID = $email;
$SERVICE_ACCOUNT_NAME = $email;
$KEY_FILE = 'path/to/p12/file';
$SPREADSHEETS_SCOPE = 'https://spreadsheets.google.com/feeds';
$key = file_get_contents($KEY_FILE);
$auth = new Google_Auth_AssertionCredentials(
$SERVICE_ACCOUNT_NAME,
array($SPREADSHEETS_SCOPE),
$key
);
$client = new Google_Client();
$client->setScopes(array($SPREADSHEETS_SCOPE));
$client->setAssertionCredentials($auth);
$client->getAuth()->refreshTokenWithAssertion();
$client->setClientId($CLIENT_ID);
$accessToken = $client->getAccessToken();
Also, I had to make sure I:
Shared my spreadsheet specifically with the email address on my service account in the code above
Synced my server's time (I'm running Vagrant CentOS so it's slightly different)
I believe you can run this code with other services beyond Spreadsheets, such as Youtube, Analytics, etc., but you will need to get the correct scope link (see $SPREADSHEETS_SCOPE above). Remember, this is only when using the Service Account on the Google Console, which means you are programmatically getting data from your code. If you are looking to have others users sign in using the API, then it's different.

Facebook PHP API not always returning event venue data

I have 3 events:
https://www.facebook.com/491594114271958 (Mad Haus)
https://www.facebook.com/569226999799343 (Deuglish)
539504962802119/ (Piffle)
All are being fetched via the PHP
$config = array();
$config['appId'] = $appId;
$config['secret'] = $secret;
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$ev = $facebook->api('/'.$id."?fields=cover,description,location,name,owner,venue",'GET');
print_r($ev);
For some reason Mad Haus and Piffle do not return venue data but Deuglish does. All events return basic data such as title, description and start time. When I run them through the Graph API explorer the venue data is returned as expected but not through the PHP API, any ideas? I can not for the life of me see the difference with these 3 events.
Thanks,
Caroline
Well, if you don't want to wait for the Facebook developers to fix the bug, you can try the same by making a simple GET request (of course along with an Access Token) to Graph API using PHP cURL. Since you are able to retrieve the venues while using the Graph API explorer, I'm pretty sure you'll be able to get them by making a simple GET request using PHP cURL.
This however involves an overhead of parsing the received JSON object and dealing with errors will be a little difficult here. But you can give it a shot.

Manipulate an organisation's calendar with Google APIs

I'm trying to find out how to integrate the Google calendar using the v3 API with an existing application for a local non-profit organization.
The intent is the following: our organisation has a general public Google calendar (with Google account) that both visitors and active volunteers can subscribe to or show in their own calendar. We are in the process of developing a simple management web application (written in PHP) in which task forces can be created and managed. The core functionality is planning meetings (and managing the notes of these meetings, but this is not relevant to the question.). Users are notified via e-mail when a meeting is planned. It would also be very nice if the planned meetings could be programmatically added to the Google calendar. I now have to do this manually for every planned meeting.
I've been reading the documentation over at developers.google.com and the Google Code page for the php library for two days now and I find myself lost in different authentication schemes, account types, keys and many more, to the point where I'm starting to think that this is not an intended use case.
To come to the core of my question: I need to access the organisations Google calendar, and be able to post events, without any human interaction. Is this even possible? If so, Which account do i need to create (web app, service account or installed app?) in the Google APIs console? How would I authenticate the application? Do I need to grant rights to the application somehow? Is there an example somewhere I can refer to (possibly with a different API?)?
From what I've seen, the critical difference with all examples in Google's documentation, is that I do not want to modify a current or end-user's calendar, but the same calendar, regardless of the logged in user. (user authentication does not happen with the Google api, we rolled our own.) The redirect/user consent mechanism isn't exactly useful for me.
In this question, the asker is trying to achieve something similar. The full answer is not provided however. I can't seem to find some of the sources referred to in the accepted response.
You should be able to make a start with the sample code from here: https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/calendar/simple.php
And referring to here: https://developers.google.com/google-apps/calendar/v3/reference/events/quickAdd#examples
Regarding authentication, you'll need to visit https://code.google.com/apis/console?api=calendar to create and obtain the relevant keys, but it's not very clear so you might need to play around a bit.
I created a "service account" for API access, which gave me the client ID and let me download a private key, but I'm not sure without trying things further how that applies to the code sample linked above.
EDIT
I just found some old code that still works which creates events in calendars using the ZendGdata-1.10.2 library:
//for Google Calendar
set_include_path('./ZendGdata-1.10.2/library');
function createAlert($message, $date) {
$google = array();
// Define credentials for the Google Calendar account
$google['GCAL_USER'] = "info#example.com";
$google['GCAL_PASS'] = "password123";
// Include Zend GData client library
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
// Get Google Calendar service name (predefined service name for calendar)
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
// Authenticate with Google Calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($google['GCAL_USER'], $google['GCAL_PASS'], $service);
date_default_timezone_set('GMT');
// Create a calendar object
$calendar = new Zend_Gdata_Calendar($client);
// Create a new event
$event = $calendar->newEventEntry();
$event->title = $calendar->newTitle($message);
// Add our message as the event content
$event->content = $calendar->newContent($message);
$when = $calendar->newWhen();
// Min time is 5 mins away
if ($date == "" || strtotime('+5 minute') >= strtotime($date)+300) {
$when->startTime = date("c", strtotime('+5 minute'));
$when->endTime = date("c", strtotime('+10 minute'));
} else {
$when->startTime = date("c", strtotime($date)+300);
$when->endTime = date("c", strtotime($date)+600);
}
$event->when = array($when);
// Setup reminders for event
$reminder = $calendar->newReminder();
$reminder->method = "all";
$when = $event->when[0];
$when->reminders = array($reminder);
// Send the new event to be added to Google Calendar
if (defined('GCAL_ID')) {
$newEvent = $calendar->insertEvent($event, 'http://www.google.com/calendar/feeds/' . $google['GCAL_ID'] . '/private/full');
} else {
$newEvent = $calendar->insertEvent($event);
}
// Check response
if (stripos($newEvent->id->text, "http://www.google.com/calendar/feeds/". str_replace('#', '%40', $google['GCAL_ID']) ."/private/full/") !== false) {
return "Sent!\n";
} else {
return $newEvent->id->text;
}
}

Exchange Server 2007 Web Services PHP Class

Does anyone know of an open source PHP class (preferably BSD or MIT license) that will interface with the MS Exchange Server 2007 Web Services via. SOAP?
I am looking for a higher level class that has functionality for sending messages via. the web service.
I had this same problem, so I started building something, here:
https://github.com/rileydutton/Exchange-Web-Services-for-PHP
It doesn't do much yet (basically just lets you get a list of email messages from the server, and send email), but it would be good enough to use as a basic starting point for doing some more complicated things.
I have abstracted out a good bit of the complexity that you would have to slog through using php-ews. If you are looking to do some raw, powerful commands with the server, I would use php-ews...this is for folks who just happen to be working with an Exchange server and want an easy way to do some basic tasks.
Oh, and it is MIT licensed.
Hope that someone finds it useful!
Here is a class that you need: php-ews (This library makes Microsoft Exchange 2007 Web Services easier to implement in PHP).
You could find it at: http://code.google.com/p/php-ews/
There is only one example but that should give you the way to implement it.
Below you can find an implementation in order to:
connect to server
get the calendar events
Note: don't forget to fill-in blank variables. You would also need to include php-ews classes files (I used the __autoload PHP function).
$host = '';
$username = '';
$password = '';
$mail = '';
$startDateEvent = ''; //ie: 2010-09-14T09:00:00
$endDateEvent = ''; //ie: 2010-09-20T17:00:00
$ews = new ExchangeWebServices($host, $username, $password);
$request = new EWSType_FindItemType();
$request->Traversal = EWSType_FolderQueryTraversalType::SHALLOW;
$request->CalendarView->StartDate = $startDateEvent;
$request->CalendarView->EndDate = $endDateEvent;
$request->CalendarView->MaxEntriesReturned = 100;
$request->CalendarView->MaxEntriesReturnedSpecified = true;
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::CALENDAR;
$request->ParentFolderIds->DistinguishedFolderId->Mailbox->EmailAddress = $mail;
$response = $ews->FindItem($request);
echo '<pre>'.print_r($response, true).'</pre>';
Exchange server supports WebDAV:
http://www.troywolf.com/articles/php/exchange_webdav_examples.php
If all you want to do is send messages, you could just use SMTP:
http://ca2.php.net/manual/en/book.mail.php
I have been researching this same issue and I have yet to find a class specific to MS Exchange. However, if you feel up to learning and building the XML yourself, you may want to have a look at the NTLM SOAP classes at http://rabaix.net/en/articles/2008/03/13/using-soap-php-with-ntlm-authentication. This will allow you to authenticate against Active Directory to make your SOAP calls, which native PHP SOAP does not allow you to do. Another decent resource that uses the same method to connect to MS CRM is http://www.reutone.com/heb/articles_internet.php?instance_id=62&actions=show&id=521.
The examples under http://www.troywolf.com/articles/php/exchange_webdav_examples.php are for Exchange 2003 not 2007.

Categories