Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

PHP Extension Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(PHP Extension) SharePoint -- Get Server Form Digest Value

Demonstrates how to get a server form digest value to be placed in the X-RequestDigest HTTP request header for POST, PUT, MERGE, and DELETE requests. A form digest value is typically valid for 1800 seconds (i.e. 30 minutes). This example persists the value to a file, and only requests a new form digest value if the existing one is near expiration.

Chilkat PHP Downloads

PHP Extension for Windows, Linux, MAC OS X,
Alpine Linux, Solaris, OpenBSD, FreeBSD,
and Linux armhf/aarch64

<?php

// The version number (9_5_0) should match version of the Chilkat extension used, omitting the micro-version number.
// For example, if using Chilkat v9.5.0.48, then include as shown here:
include("chilkat_9_5_0.php");

//  This requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  First, let's see if we already have a persisted form digest value
//  that hasn't yet expired.
$fac = new CkFileAccess();
$xml = new CkXml();

//  My example code (below) persists the form digest XML in this format:
// 
//  	<savedFormDigestValue>
//  	<d:ExpireDateTime>2017-04-12T20:46:39Z</d:ExpireDateTime>
//  	<d:FormDigestValue>0x3059FFB920651834540F3E6792EA73F5746B302E953FF4E808E485DB1E6C2836C7CF924644995F092453B02A94DE14A7962674B7B16780AF16EAFB8C246BCDC7,12 Apr 2017 17:08:22 -0000</d:FormDigestValue>
//  	</savedFormDigestValue>
// 

$dtExpire = new CkDateTime();
$dtNow = new CkDateTime();

$formDigestXmlFile = 'qa_data/sharepoint/savedFormDigestValue.xml';
if ($fac->FileExists($formDigestXmlFile) == true) {

    $xml->LoadXmlFile($formDigestXmlFile);

    //  Get the expire date/time
    $dtExpire->SetFromTimestamp($xml->getChildContent('d:ExpireDateTime'));

    //  Get the current date/time
    $dtNow->SetFromCurrentSystemTime();

    //  Get both times as Unix time values
    $tNow = $dtNow->GetAsUnixTime(false);
    $tExpire = $dtExpire->GetAsUnixTime(false);

    //  If tNow >= tExpire, then fall through.
    //  Otherwise, just use the cached digest value.
    if ($tNow < $tExpire) {
        print 'Cached digest value is not yet expired.' . "\n";
        print 'X-RequestDigest: ' . $xml->getChildContent('d:FormDigestValue') . "\n";
        exit;
    }

}

//  If we got to this point, the cached digest value either does not exist, or expired.

$http = new CkHttp();

//  If SharePoint Windows classic authentication is used, then set the
//  Login, Password, LoginDomain, and NtlmAuth properties.
$http->put_Login('SHAREPOINT_USERNAME');
$http->put_Password('SHAREPOINT_PASSWORD');
$http->put_LoginDomain('SHAREPOINT_NTLM_DOMAIN');
$http->put_NtlmAuth(true);

//  The more common case is to use SharePoint Online authentication (via the SPOIDCRL cookie).
//  If so, do not set Login, Password, LoginDomain, and NtlmAuth, and instead
//  establish the cookie as shown at SharePoint Online Authentication

//  When creating, updating, and deleting SharePoint entities, we'll need
//  to first get the server's form digest value to send in the X-RequestDigest header.
//  This can be retrieved by making a POST request with an empty body to
//  http://<site url>/_api/contextinfo and extracting the value of the
//  d:FormDigestValue node in the XML that the contextinfo endpoint returns.

//  Note: The last argument ("utf-8") is meaningless here because the body is empty.
// resp is a CkHttpResponse
$resp = $http->PostXml('https://SHAREPOINT_HTTPS_DOMAIN/_api/contextinfo','','utf-8');
if ($http->get_LastMethodSuccess() != true) {
    print $http->lastErrorText() . "\n";
    exit;
}

if ($resp->get_StatusCode() != 200) {
    //  A response status code not equal to 200 indicates failure.
    print 'Response status code = ' . $resp->get_StatusCode() . "\n";
    print 'Response body:' . "\n";
    print $resp->bodyStr() . "\n";

    exit;
}

$xml->LoadXml($resp->bodyStr());

//  The response XML looks like this:

//  <?xml version="1.0" encoding="utf-8" ?>
//  <d:GetContextWebInformation xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" m:type="SP.ContextWebInformation">
//      <d:FormDigestTimeoutSeconds m:type="Edm.Int32">1800</d:FormDigestTimeoutSeconds>
//      <d:FormDigestValue>0x3059FFB920651834540F3E6792EA73F5746B302E953FF4E808E485DB1E6C2836C7CF924644995F092453B02A94DE14A7962674B7B16780AF16EAFB8C246BCDC7,12 Apr 2017 17:08:22 -0000</d:FormDigestValue>
//      <d:LibraryVersion>15.0.4569.1000</d:LibraryVersion>
//      <d:SiteFullUrl>https://SHAREPOINT_HTTPS_DOMAIN</d:SiteFullUrl>
//      <d:SupportedSchemaVersions m:type="Collection(Edm.String)">
//          <d:element>14.0.0.0</d:element>
//          <d:element>15.0.0.0</d:element>
//      </d:SupportedSchemaVersions>
//      <d:WebFullUrl>https://SHAREPOINT_HTTPS_DOMAIN</d:WebFullUrl>
//  </d:GetContextWebInformation>
// 

//  Cache the digest value, and also an expiration time.  If this code is run again
//  before the digest expires, we'll just get it from the file.
$xml2 = new CkXml();

$xml2->put_Tag('savedFormDigestValue');
$xml2->NewChild2('d:FormDigestValue',$xml->getChildContent('d:FormDigestValue'));

$timeoutInSec = $xml->GetChildIntValue('d:FormDigestTimeoutSeconds');
print 'Timeout in seconds = ' . $timeoutInSec . "\n";

//  Convert this to an expire timestamp.
//  Let's make it expire 30 seconds prior to the actual timeout, just to be safe.
if ($timeoutInSec > 30) {
    $timeoutInSec = $timeoutInSec - 30;
}

$dtExpire->SetFromCurrentSystemTime();
$dtExpire->AddSeconds($timeoutInSec);
$xml2->NewChild2('d:ExpireDateTime',$dtExpire->getAsTimestamp(false));

//  Persist the digest and expire time to a file.
$xml2->SaveXml($formDigestXmlFile);

print 'Here is the new form digest value:' . "\n";
print 'X-RequestDigest: ' . $xml->getChildContent('d:FormDigestValue') . "\n";

?>

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.