PHP code example of vgrem / php-spo

1. Go to this page and download the library: Download vgrem/php-spo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vgrem / php-spo example snippets



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

  $credentials = new ClientCredential("{clientId}", "{clientSecret}");
  $ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);


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


$tenant = "{tenant}.onmicrosoft.com"; //tenant id or name
$privateKeyPath = "-- path to private.key file--"
$privateKey = file_get_contents($privateKeyPath);

$ctx = (new ClientContext("{siteUrl}"))->withClientCertificate(
    $tenant, "{clientId}", $privateKey, "{thumbprint}");


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

  $credentials = new UserCredentials("{userName}", "{password}");
  $ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);

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

   $credentials = new UserCredentials("{userName}", "{password}");
   $ctx = (new ClientContext("{siteUrl}"))->withNtlm($credentials);


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";
}


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";
}

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)->executeQuery();
print "Task {$item->getProperty('Title')} has been created.\r\n";

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()->executeQuery();

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()->executeQuery();


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();


 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();

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()->get()->executeQuery();
print $drive->getWebUrl();

json
{
    "grem/php-spo": "^3"
    }
}