PHP code example of appopcen / php-spo

1. Go to this page and download the library: Download appopcen/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/ */

    

appopcen / php-spo example snippets





$authCtx = new AuthenticationContext($Url);
$authCtx->acquireTokenForUser($UserName,$Password); //authenticate

$ctx = new ClientContext($Url,$authCtx); //initialize REST client    
$web = $ctx->getWeb();
$list = $web->getLists()->getByTitle($listTitle); //init List resource
$items = $list->getItems();  //prepare a query to retrieve from the 
$ctx->load($items);  //save a query to retrieve list items from the server 
$ctx->executeQuery(); //submit query to SharePoint Online REST service
foreach( $items->getData() as $item ) {
    print "Task: '{$item->Title}'\r\n";
}

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

$listTitle = 'Tasks';
$itemToDeleteId = 1;
$list = $ctx->getWeb()->getLists()->getByTitle($listTitle);
$listItem = $list->getItemById($itemToDeleteId);
$listItem->deleteObject();
$ctx->executeQuery();

$listTitle = 'Tasks';
$itemToUpdateId = 1;
$list = $ctx->getWeb()->getLists()->getByTitle($listTitle);
$listItem = $list->getItemById($itemId);
$listItem->setProperty('PercentComplete',1);
$listItem->update();
$ctx->executeQuery();


$authCtx = new NetworkCredentialContext($UserName,$Password); //using Basic Auth scheme (for v1 API only)
$ctx = new OutlookClient($authCtx); //initialize OutlookServices client
$message = $ctx->getMe()->getMessages()->createMessage(); //create a Message resource
//set Message properties
$message->Subject = "--subject--";
$message->Body = new ItemBody(BodyType::Text,"--Content goes here--");
$message->ToRecipients = array(
     new Recipient(new EmailAddress("Jon Doe","[email protected]"))
);
$ctx->executeQuery();


$authCtx = new NetworkCredentialContext($UserName,$Password); //using Basic Auth scheme (for v1 API only)
$ctx = new OutlookClient($authCtx); //initialize OutlookServices client
$messages = $ctx->getMe()->getMessages();
$ctx->load($messages);
$ctx->executeQuery();
//print messages subjects
foreach ($messages->getData() as $curMessage){
   print $curMessage->Subject;
}


$authCtx = new NetworkCredentialContext($UserName,$Password); //using Basic Auth scheme (for v1 API only)
$ctx = new OutlookClient($authCtx); //initialize OutlookServices client
$message = $ctx->getMe()->getMessages()->createMessage(); //create a Message resource
//set Message properties
$message->Subject = "--subject--";
$message->Body = new ItemBody(BodyType::Text,"--Content goes here--");
$message->ToRecipients = array(
     new Recipient(new EmailAddress("Jon Doe","[email protected]"))
);
$ctx->getMe()->sendEmail($message,false); //send a Message
$ctx->executeQuery();