3namespace ceLTIc\LTI\Http;
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();
37 if (count(preg_grep(
"/^Accept:/i", $message->requestHeaders)) === 0) {
38 $message->requestHeaders[] =
'Accept: */*';
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';
44 curl_setopt($ch, CURLOPT_HTTPHEADER, $message->requestHeaders);
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);
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);
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);
67 $message->responseHeaders = trim($chRespSplit[0]);
68 if (count($chRespSplit) > 1) {
69 $message->response = $chRespSplit[1];
71 $message->response =
'';
73 $message->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
74 $message->ok = ($message->status >= 100) && ($message->status < 400);
76 $message->error = curl_error($ch);
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.