Giter Site home page Giter Site logo

jan-bures / phpspo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vgrem/phpspo

0.0 0.0 0.0 19.5 MB

Office 365 Library for PHP. It allows to performs CRUD operations against Office 365 resources via an REST/OData based API

License: MIT License

PHP 100.00%

phpspo's Introduction

About

Office 365 Library for PHP. A REST/OData based client library for Office 365.

Usage

  1. Installation
  2. Working with SharePoint API
  3. Working with Teams API
  4. Working with Outlook API
  5. Working with OneDrive API

Status

Total Downloads Latest Stable Version Build Status License

Installation

You can use Composer or simply Download the Release

Composer

The preferred method is via composer. Follow the installation instructions if you do not already have composer installed.

Once composer installed, execute the following command in your project root to install this library:

composer require vgrem/php-spo

or via composer.json file:

{
    "require": {
        "vgrem/php-spo": "^2.4"
    }
}

Finally, be sure to include the autoloader:

require_once '/path/to/your-project/vendor/autoload.php';

Requirements

PHP version: PHP 5.5 or later

Working with SharePoint API

The list of supported SharePoint versions:

  • SharePoint Online and OneDrive for Business
  • SharePoint On-Premises (2013-2019)

Authentication

The following auth flows supported:

  use Office365\Runtime\Auth\ClientCredential;
  use Office365\SharePoint\ClientContext;
  
  $credentials = new ClientCredential("{clientId}", "{clientSecret}");
  $ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);
  • user credentials auth:
  
  use Office365\Runtime\Auth\UserCredentials;
  use Office365\SharePoint\ClientContext;
  
  $credentials = new UserCredentials("{userName}", "{password}");
  $ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);
  • NTLM auth (for SharePoint On-Premises):
   use Office365\Runtime\Auth\UserCredentials;
   use Office365\SharePoint\ClientContext;
  
   $credentials = new UserCredentials("{userName}", "{password}");
   $ctx = (new ClientContext("{siteUrl}"))->withNtlm($credentials);
  

Examples

The following examples demonstrates how to perform basic CRUD operations against SharePoint list item resources:

Example 1. How to read SharePoint list items:

use Office365\SharePoint\ClientContext;
use Office365\Runtime\Auth\ClientCredential;
use Office365\SharePoint\ListItem;

$credentials = new ClientCredential("{client-id}", "{client-secret}");
$client = (new ClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials);     

$web = $client->getWeb();
$list = $web->getLists()->getByTitle("{list-title}"); //init List resource
$items = $list->getItems();  //prepare a query to retrieve from the 
$client->load($items);  //save a query to retrieve list items from the server 
$client->executeQuery(); //submit query to SharePoint server
/** @var ListItem $item */
foreach($items as $item) {
    print "Task: {$item->getProperty('Title')}\r\n";
}

or via fluent API syntax:

use Office365\SharePoint\ClientContext;
use Office365\Runtime\Auth\ClientCredential;
use Office365\SharePoint\ListItem;

$credentials = new ClientCredential("{client-id}", "{client-secret}");
$client = (new ClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials);     

$items = $client->getWeb()
                ->getLists()
                ->getByTitle("{list-title}") 
                ->getItems()
                ->get()
                ->executeQuery();      
/** @var ListItem $item */
foreach($items as $item) {
    print "Task: {$item->getProperty('Title')}\r\n";
}

Example 2. How to create SharePoint list item:

use Office365\SharePoint\ClientContext;
use Office365\Runtime\Auth\ClientCredential;

$credentials = new ClientCredential("{client-id}", "{client-secret}");
$client = (new ClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials);

$list = $client->getWeb()->getLists()->getByTitle("Tasks");
$itemProperties = array('Title' => 'Order Approval', 'Body' => 'Order approval task');
$item = $list->addItem($itemProperties);
$client->executeQuery();
print "Task {$item->getProperty('Title')} has been created.\r\n";

Example 3. How to delete a SharePoint list item:

use Office365\SharePoint\ClientContext;
use Office365\Runtime\Auth\ClientCredential;

$credentials = new ClientCredential("{client-id}", "{client-secret}");
$client = (new ClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials);

$list = $client->getWeb()->getLists()->getByTitle("Tasks");
$listItem = $list->getItemById("{item-id-to-delete}");
$listItem->deleteObject();
$client->executeQuery();

Example 4. How to update SharePoint list item:

use Office365\SharePoint\ClientContext;
use Office365\Runtime\Auth\ClientCredential;

$credentials = new ClientCredential("{client-id}", "{client-secret}");
$client = (new ClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials);

$list = $client->getWeb()->getLists()->getByTitle("Tasks");
$listItem = $list->getItemById("{item-id-to-update}");
$listItem->setProperty('PercentComplete',1);
$listItem->update();
$client->executeQuery();

Working with Teams API

Example: create a Team

The following is an example of a minimal request to create a Team (via delegated permissions)

use Office365\GraphServiceClient;
use Office365\Runtime\Auth\AADTokenProvider;
use Office365\Runtime\Auth\UserCredentials;

function acquireToken()
{
    $tenant = "{tenant}.onmicrosoft.com";
    $resource = "https://graph.microsoft.com";
  
    $provider = new AADTokenProvider($tenant);
    return $provider->acquireTokenForPassword($resource, "{clientId}",
        new UserCredentials("{UserName}", "{Password}"));
}

$client = new GraphServiceClient("acquireToken");
$teamName = "My Sample Team";
$newTeam = $client->getTeams()->add($teamName)->executeQuery();

Working with Outlook API

Supported list of APIs:

The following example demonstrates how to send a message via Outlook Mail API:

 use Office365\GraphServiceClient;
 use Office365\Outlook\Message;
 use Office365\Outlook\ItemBody;
 use Office365\Outlook\BodyType;
 use Office365\Outlook\EmailAddress;
 use Office365\Runtime\Auth\AADTokenProvider;
 use Office365\Runtime\Auth\UserCredentials;

function acquireToken()
{
    $tenant = "{tenant}.onmicrosoft.com";
    $resource = "https://graph.microsoft.com";
  
    $provider = new AADTokenProvider($tenant);
    return $provider->acquireTokenForPassword($resource, "{clientId}",
        new UserCredentials("{UserName}", "{Password}"));
}

$client = new GraphServiceClient("acquireToken");
/** @var Message $message */
$message = $client->getMe()->getMessages()->createType();
$message->setSubject("Meet for lunch?");
$message->setBody(new ItemBody(BodyType::Text,"The new cafeteria is open."));
$message->setToRecipients([new EmailAddress(null,"[email protected]")]);
$client->getMe()->sendEmail($message,true)->executeQuery();

Working with OneDrive API

The following example demonstrates how retrieve My drive Url via OneDrive API:

use Office365\GraphServiceClient;
use Office365\Runtime\Auth\AADTokenProvider;
use Office365\Runtime\Auth\UserCredentials;


function acquireToken()
{
    $tenant = "{tenant}.onmicrosoft.com";
    $resource = "https://graph.microsoft.com";
  
    $provider = new AADTokenProvider($tenant);
    return $provider->acquireTokenForPassword($resource, "{clientId}",
        new UserCredentials("{UserName}", "{Password}"));
}

$client = new GraphServiceClient("acquireToken");
$drive = $client->getMe()->getDrive();
$client->load($drive);
$client->executeQuery();
print $drive->getWebUrl();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.