LTI Integration Library  3.1.0
PHP class library for building LTI integrations
Service.php
Go to the documentation of this file.
1 <?php
2 
3 namespace ceLTIc\LTI\Service;
4 
5 use ceLTIc\LTI;
8 
17 class Service
18 {
19 
25  public $unsigned = false;
26 
32  protected $endpoint;
33 
39  private $consumer;
40 
46  private $mediaType;
47 
53  private $http = null;
54 
62  public function __construct($consumer, $endpoint, $mediaType)
63  {
64  $this->consumer = $consumer;
65  $this->endpoint = $endpoint;
66  $this->mediaType = $mediaType;
67  }
68 
78  public function send($method, $parameters = array(), $body = null)
79  {
80  $url = $this->endpoint;
81  if (!empty($parameters)) {
82  if (strpos($url, '?') === false) {
83  $sep = '?';
84  } else {
85  $sep = '&';
86  }
87  foreach ($parameters as $name => $value) {
88  $url .= $sep . urlencode($name) . '=' . urlencode($value);
89  $sep = '&';
90  }
91  }
92  if (!$this->unsigned) {
93  $header = $this->consumer->signServiceRequest($url, $method, $this->mediaType, $body);
94  } else {
95  $header = null;
96  }
97 
98 // Connect to tool consumer
99  $this->http = new HTTPMessage($url, $method, $body, $header);
100 // Parse JSON response
101  if ($this->http->send() && !empty($this->http->response)) {
102  $this->http->responseJson = json_decode($this->http->response);
103  $this->http->ok = !is_null($this->http->responseJson);
104  }
105 
106  return $this->http;
107  }
108 
114  public function getHTTPMessage()
115  {
116  return $this->http;
117  }
118 
119 }
$endpoint
Service endpoint.
Definition: Service.php:32
$unsigned
Whether service request should be sent unsigned.
Definition: Service.php:25
Class to implement a service.
Definition: Service.php:17
__construct($consumer, $endpoint, $mediaType)
Class constructor.
Definition: Service.php:62
Class to represent a tool consumer.
send($method, $parameters=array(), $body=null)
Send a service request.
Definition: Service.php:78
Class to represent an HTTP message request.
Definition: HTTPMessage.php:16
getHTTPMessage()
Get HTTPMessage object for last request.
Definition: Service.php:114