PHP Integration Kit

Sample code

A simple example of this mode of operation is demonstrated in the PHP code below:

 <?php
    include_once “pingidentity/opentoken/helpers/opentoken.php”
    if ($opentoken_haveValidToken == true)
    {
       echo “Welcome “, $opentoken_subject, “!<p>”;
       print_r($opentoken_values);
    }
    else
    {
       echo “You are not logged in: “, $opentoken_lastError, “<p>”;
    }
 ?>

As with the IdP, you can use the pingidentity/opentoken/agent.php API to read tokens directly. The agent.php API is a PHP object that provides access to functionality for reading an OpenToken from a given HTTP request.

Instantiating the agent object is done simply by using the PHP Agent Namespace and invoking the constructor, as in the example below:

 <?php
    use pingidentity\opentoken\agent;
    $myagent = new Agent();
 ?>

When the Agent object is instantiated, it uses the config.php file to find the configuration data generated when the OpenToken Adapter was configured. This configuration data includes the name of the cookie that the agent object will read. If the file specified in config.php is not found, the Agent constructor will throw an exception.

The readTokenFromHTTPRequest method inspects the cookie (or query parameters, depending on the configuration of the agent instance) and decodes the OpenToken, returning an array of attributes or null if no token is found or an error is encountered. In the case of an error, the lastError attribute will contain the error message.

The following code demonstrates the use of this method:

 <?php
    # Use and instantiate an agent
    use pingidentity\opentoken\agent;
    $myagent = new Agent();

    # Use the read method to extract the token values
    $myvalues = $myagent->readTokenFromHTTPRequest();
    if ($myvalues)
    {
       # Found some values – print each of them
       foreach ($myvalues as $key => $value)
       {        echo $key . “ = “ . $value . “<br>”;
       }
    }
    else
    {
       # No values – print the error message
       echo “Error reading token: “ . $myagent->lastError;
    }
 ?>