Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace IMSGlobal\LTI\ToolProvider\Service;
   4  
   5  use IMSGlobal\LTI\ToolProvider;
   6  use IMSGlobal\LTI\HTTPMessage;
   7  
   8  /**
   9   * Class to implement a service
  10   *
  11   * @author  Stephen P Vickers <svickers@imsglobal.org>
  12   * @copyright  IMS Global Learning Consortium Inc
  13   * @date  2016
  14   * @version 3.0.0
  15   * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  16   */
  17  class Service
  18  {
  19  
  20  /**
  21   * Whether service request should be sent unsigned.
  22   *
  23   * @var boolean $unsigned
  24   */
  25      public $unsigned = false;
  26  
  27  /**
  28   * Service endpoint.
  29   *
  30   * @var string $endpoint
  31   */
  32      protected $endpoint;
  33  /**
  34   * Tool Consumer for this service request.
  35   *
  36   * @var ToolConsumer $consumer
  37   */
  38      private $consumer;
  39  /**
  40   * Media type of message body.
  41   *
  42   * @var string $mediaType
  43   */
  44      private $mediaType;
  45  
  46  /**
  47   * Class constructor.
  48   *
  49   * @param ToolConsumer $consumer   Tool consumer object for this service request
  50   * @param string       $endpoint   Service endpoint
  51   * @param string       $mediaType  Media type of message body
  52   */
  53      public function __construct($consumer, $endpoint, $mediaType)
  54      {
  55  
  56          $this->consumer = $consumer;
  57          $this->endpoint = $endpoint;
  58          $this->mediaType = $mediaType;
  59  
  60      }
  61  
  62  /**
  63   * Send a service request.
  64   *
  65   * @param string  $method      The action type constant (optional, default is GET)
  66   * @param array   $parameters  Query parameters to add to endpoint (optional, default is none)
  67   * @param string  $body        Body of request (optional, default is null)
  68   *
  69   * @return HTTPMessage HTTP object containing request and response details
  70   */
  71      public function send($method, $parameters = array(), $body = null)
  72      {
  73  
  74          $url = $this->endpoint;
  75          if (!empty($parameters)) {
  76              if (strpos($url, '?') === false) {
  77                  $sep = '?';
  78              } else {
  79                  $sep = '&';
  80              }
  81              foreach ($parameters as $name => $value) {
  82                  $url .= $sep . urlencode($name) . '=' . urlencode($value);
  83                  $sep = '&';
  84              }
  85          }
  86          if (!$this->unsigned) {
  87              $header = ToolProvider\ToolConsumer::addSignature($url, $this->consumer->getKey(), $this->consumer->secret, $body, $method, $this->mediaType);
  88          } else {
  89              $header = null;
  90          }
  91  
  92  // Connect to tool consumer
  93          $http = new HTTPMessage($url, $method, $body, $header);
  94  // Parse JSON response
  95          if ($http->send() && !empty($http->response)) {
  96              $http->responseJson = json_decode($http->response);
  97              $http->ok = !is_null($http->responseJson);
  98          }
  99  
 100          return $http;
 101  
 102      }
 103  
 104  }