LTI Integration Library  3.1.0
PHP class library for building LTI integrations
CurlClient.php
Go to the documentation of this file.
1 <?php
2 
3 namespace ceLTIc\LTI\Http;
4 
6 
15 class CurlClient implements ClientInterface
16 {
17 
25  public function send(HTTPMessage $message)
26  {
27  $ch = curl_init();
28  curl_setopt($ch, CURLOPT_URL, $message->getUrl());
29  if (!empty($message->requestHeaders)) {
30  curl_setopt($ch, CURLOPT_HTTPHEADER, $message->requestHeaders);
31  } else {
32  curl_setopt($ch, CURLOPT_HEADER, 0);
33  }
34  if ($message->getMethod() === 'POST') {
35  curl_setopt($ch, CURLOPT_POST, true);
36  curl_setopt($ch, CURLOPT_POSTFIELDS, $message->request);
37  } elseif ($message->getMethod() !== 'GET') {
38  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $message->getMethod());
39  if (!is_null($message->request)) {
40  curl_setopt($ch, CURLOPT_POSTFIELDS, $message->request);
41  }
42  }
43  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
44  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
45  curl_setopt($ch, CURLOPT_HEADER, true);
46  $chResp = curl_exec($ch);
47  $message->ok = $chResp !== false;
48  if ($message->ok) {
49  $chResp = str_replace("\r\n", "\n", $chResp);
50  $chRespSplit = explode("\n\n", $chResp, 2);
51  if ((count($chRespSplit) > 1) && (substr($chRespSplit[1], 0, 5) === 'HTTP/')) {
52  $chRespSplit = explode("\n\n", $chRespSplit[1], 2);
53  }
54  $message->responseHeaders = $chRespSplit[0];
55  $message->response = $chRespSplit[1];
56  $message->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
57  $message->ok = $message->status < 400;
58  if (!$message->ok) {
59  $message->error = curl_error($ch);
60  }
61  }
62  $message->requestHeaders = str_replace("\r\n", "\n", curl_getinfo($ch, CURLINFO_HEADER_OUT));
63  curl_close($ch);
64 
65  return $message->ok;
66  }
67 
68 }
getUrl()
Get the target URL for the request.
getMethod()
Get the HTTP method for the request.
send(HTTPMessage $message)
Send the request to the target URL.
Definition: CurlClient.php:25
Class to implement the HTTP message interface using the Curl library.
Definition: CurlClient.php:15
Class to represent an HTTP message request.
Definition: HTTPMessage.php:16
Interface to represent an HTTP message client.