LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
CurlClient.php
1<?php
2
3namespace ceLTIc\LTI\Http;
4
13{
14
20 public static $httpVersion = null;
21
29 public function send(HttpMessage $message)
30 {
31 $ch = curl_init();
32 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
33 curl_setopt($ch, CURLOPT_URL, $message->getUrl());
34 if (!is_array($message->requestHeaders)) {
35 $message->requestHeaders = array();
36 }
37 if (count(preg_grep("/^Accept:/i", $message->requestHeaders)) === 0) {
38 $message->requestHeaders[] = 'Accept: */*';
39 }
40 if (($message->getMethod() !== 'GET') && !is_null($message->request) &&
41 (count(preg_grep("/^Content-Type:/i", $message->requestHeaders)) === 0)) {
42 $message->requestHeaders[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
43 }
44 curl_setopt($ch, CURLOPT_HTTPHEADER, $message->requestHeaders);
45 if ($message->getMethod() === 'POST') {
46 curl_setopt($ch, CURLOPT_POST, true);
47 curl_setopt($ch, CURLOPT_POSTFIELDS, $message->request);
48 } elseif ($message->getMethod() !== 'GET') {
49 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $message->getMethod());
50 if (!is_null($message->request)) {
51 curl_setopt($ch, CURLOPT_POSTFIELDS, $message->request);
52 }
53 }
54 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
55 curl_setopt($ch, CURLINFO_HEADER_OUT, true);
56 curl_setopt($ch, CURLOPT_HEADER, true);
57 if (!empty(self::$httpVersion)) {
58 curl_setopt($ch, CURLOPT_HTTP_VERSION, self::$httpVersion);
59 }
60 $chResp = curl_exec($ch);
61 $message->requestHeaders = trim(str_replace("\r\n", "\n", curl_getinfo($ch, CURLINFO_HEADER_OUT)));
62 $chResp = str_replace("\r\n", "\n", $chResp);
63 $chRespSplit = explode("\n\n", $chResp, 2);
64 if ((count($chRespSplit) > 1) && (substr($chRespSplit[1], 0, 5) === 'HTTP/')) {
65 $chRespSplit = explode("\n\n", $chRespSplit[1], 2);
66 }
67 $message->responseHeaders = trim($chRespSplit[0]);
68 if (count($chRespSplit) > 1) {
69 $message->response = $chRespSplit[1];
70 } else {
71 $message->response = '';
72 }
73 $message->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
74 $message->ok = ($message->status >= 100) && ($message->status < 400);
75 if (!$message->ok) {
76 $message->error = curl_error($ch);
77 }
78 curl_close($ch);
79
80 return $message->ok;
81 }
82
83}
Class to implement the HTTP message interface using the Curl library.
static $httpVersion
The HTTP version to be used.
send(HttpMessage $message)
Send the request to the target URL.
Class to represent an HTTP message request.
getUrl()
Get the target URL for the request.
getMethod()
Get the HTTP method for the request.
Interface to represent an HTTP message client.