Basic LTI Tutorial Using PHP

Exploring the Code

If you followed the instructions above, you should be located in your demo folder. List out its contents. At this point, you should see a file called Vagrantfile and a puphpet folder. You can ignore these for now–they are responsible for setting up and running your virtual environment. Instead, change into the html folder that is present.

Within the html folder are several .php files, an index.html file, and several folders. You can ignore most of the folders–they contain images and css files–but note the ims-blti folder. Your Basic LTI libraries are stored in there.

TIP! You might have noticed another html folder and a default folder among all those other files and folders. These folders were automatically created by your virtual machine and can be safely ignored.

The .php files include lms.php (the sample LMS you fired up earlier), misc.php (some miscellaneous code used by the sample LMS), and tool-helloworld.php. The latter file is the one you will be writing!

Let’s Write Some Code

Open up tool-helloworld.php in your favorite text editor. It should be beautifully blank, so type in the following:

<?php
error_reporting(E_ALL & ~E_NOTICE);
ini_set("display_errors", 1);

The above is some bare bones PHP that sets some error reporting. Now add the following code:

require_once 'ims-blti/blti.php';
$lti = new BLTI("secret", false, false);

The above code first loads the Basic LTI library, then instantiates a new LTI object. The parameters of the object indicate, respectively, to set its OAuth secret to ‘secret’, do not set a session, and do not redirect to another application. (If you want to know what all of this really means, it’s best to explore the PHP libraries and see what that code is up to; but it’s not important at the moment.)

Now we will write some more standard PHP and HTML that will begin outputting the web page:

session_start();
header('Content-Type: text/html; charset=utf-8');  
?>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Building Tools With The Learning Tools Operability Specification</title>
  </head>
  
  <body>

At this point, we want to check if the LTI launch is valid. If so, we print out all the POST parameters associated with this HTTP request:

 <?php
    if ($lti->valid) {
  ?>
     <h2>Hello, World!</h2>
     <p>We have implemented a basic LTI tool!</p>
     <h3>A basic dump of POST parameters:</h3>
     <pre>  
  <?php
    foreach($_POST as $key => $value) {
        print "$key=$value\n";
    }
  ?>
      </pre>

Finally, if the Basic LTI launch is not valid, we dump an error message. We then close out the HTML:

 <?php
    } else {
  ?>
    <h2>This was not a valid LTI launch</h2>
    <p>Error message: <?= $lti->message ?></p>
  <?php
    }
  ?>
  </body>
  
</html>

Taken all together, our code should look like this:

<?php
error_reporting(E_ALL & ~E_NOTICE);
ini_set("display_errors", 1);
 
require_once 'ims-blti/blti.php';
$lti = new BLTI("secret", false, false);

session_start();
header('Content-Type: text/html; charset=utf-8'); 
?>
 
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Building Tools With The Learning Tools Operability Specification</title>
  </head>
   
  <body>
  <?php
   if ($lti->valid) {
 ?>
    <h2>Hello, World!</h2>
    <p>We have implemented a basic LTI tool!</p>
    <h3>A basic dump of POST parameters:</h3>
    <pre> 
 <?php
   foreach($_POST as $key => $value) {
       print "$key=$value\n";
   }
 ?>
     </pre>
 <?php
    } else {
  ?>
    <h2>This was not a valid LTI launch</h2>
    <p>Error message: <?= $lti->message ?></p>
  <?php
    }
  ?>
  </body>
   
</html>

Pages: 1 2 3 4

Leave a comment