phpspo

repository·master·Indexed 18 days ago

https://github.com/vgrem/phpspo

A REST/OData based PHP client library for interacting with Microsoft 365 services, including SharePoint, Teams, Outlook, and OneDrive. It supports multiple authentication methods including Client Credentials, Client Certificates, User Credentials, and NTLM for SharePoint On-Premises. The library provides capabilities for CRUD operations on SharePoint list items, managing Microsoft Teams via Graph API, sending emails via Outlook Mail API, and interacting with Microsoft 365 directory entities such as Applications, Persons, and Sign-In activities.

Tokens
8.9K
Snippets
26
Records
33
Agent score
63%

What's inside phpspo

  1. Install required PHP extensions

    master

    To use this project, ensure the following PHP extensions are enabled in your environment:

    • PHP cURL: For making network requests.
    • DOM: For XML/HTML parsing.
    • Multibyte String (mbstring): For handling multi-byte character encodings.

    On Ubuntu/Linux, you can install them using apt-get:

    apt-get install php-curl
    apt-get install php-dom
    apt-get install php-mbstring
  2. Perform CRUD operations on SharePoint List Items

    master

    The library provides methods to interact with SharePoint lists. Note that most operations require calling executeQuery() to submit the request to the server.

    // Read list items
    $list = $client->getWeb()->getLists()->getByTitle("{list-title}");
    $items = $list->getItems();
    $client->load($items);
    $client->executeQuery();
    
    // Create a list item
    $itemProperties = array('Title' => 'New Item', 'Body' => 'Content');
    $item = $list->addItem($itemProperties)->executeQuery();
    
    // Update a list item
    $listItem = $list->getItemById("{item-id}");
    $listItem->setProperty('PercentComplete', 1);
    $listItem->update()->executeQuery();
    
    // Delete a list item
    $listItem = $list->getItemById("{item-id}");
    $listItem->deleteObject()->executeQuery();
  3. Run the test suite

    master

    Execute the project's test suite using PHPUnit. You must use the provided bootstrap file and target the tests/sharepoint directory.

    vendor/bin/phpunit --bootstrap tests/bootstrap.php --no-configuration tests/sharepoint
  4. Retrieve My Drive URL via OneDrive API

    master

    Access OneDrive information using the Graph API through the GraphServiceClient.

    use Office365\GraphServiceClient;
    use Office365\Runtime\Auth\AADTokenProvider;
    use Office365\Runtime\Auth\UserCredentials;
    
    // ... acquireToken() implementation ...
    
    $client = new GraphServiceClient("acquireToken");
    $drive = $client->getMe()->getDrive()->get()->executeQuery();
    print $drive->getWebUrl();
  5. Send an email via Outlook Mail API

    master

    Use the GraphServiceClient and Office365\Outlook classes to construct and send messages.

    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;
    
    // ... acquireToken() implementation as shown in Teams example ...
    
    $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,"fannyd@contoso.onmicrosoft.com")]);
    $client->getMe()->sendEmail($message,true)->executeQuery();
  6. Create a Microsoft Team via Graph API

    master

    To create a Team, you must first acquire an AAD token using an AADTokenProvider and then use the GraphServiceClient to call the Teams 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");
    $teamName = "My Sample Team";
    $newTeam = $client->getTeams()->add($teamName)->executeQuery();
  7. Authenticate with SharePoint using a Client Certificate

    master

    Use an App Principal with a client certificate for enhanced security. You will need the tenant name, client ID, the private key content, and the certificate thumbprint.

    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}");
  8. Authenticate with SharePoint using User Credentials

    master

    Authenticate using standard username and password credentials.

    use Office365\Runtime\Auth\UserCredentials;
    use Office365\SharePoint\ClientContext;
    
    $credentials = new UserCredentials("{userName}", "{password}");
    $ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);
  9. Authenticate with SharePoint using Client Credentials

    master

    Use an App Principal with a Client ID and Client Secret to authenticate with SharePoint Online or On-Premises. This is suitable for service-to-service communication.

    use Office365\Runtime\Auth\ClientCredential;
    use Office365\SharePoint\ClientContext;
    
    $credentials = new ClientCredential("{clientId}", "{clientSecret}");
    $ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);
  10. Authenticate with SharePoint On-Premises using NTLM

    master

    For SharePoint On-Premises environments, use NTLM authentication with user credentials.

    use Office365\Runtime\Auth\UserCredentials;
    use Office365\SharePoint\ClientContext;
    
    $credentials = new UserCredentials("{userName}", "{password}");
    $ctx = (new ClientContext("{siteUrl}"))->withNtlm($credentials);
  11. Access specialized application configurations

    master

    An Application object in the Microsoft 365 directory can act as different types of clients. You can retrieve the specific configuration object for the application type using the following methods:

    MethodReturnsDescription
    getApi()ApiApplicationConfiguration for API-based applications.
    getPublicClient()PublicClientApplicationConfiguration for public client applications.
    getWeb()WebApplicationConfiguration for web-based applications.
    getInfo()InformationalUrlInformation about the application's URL.
    getOptionalClaims()OptionalClaimsConfiguration for optional claims.
    getParentalControlSettings()ParentalControlSettingsSettings for parental controls.