ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
CurlClient.php
Go to the documentation of this file.
1 <?php
2 
20 
28 class CurlClient implements ClientInterface
29 {
37  public function send(HttpMessage $message): bool
38  {
39  $ch = curl_init();
40  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
41  curl_setopt($ch, CURLOPT_URL, $message->getUrl());
42  if (!empty($message->requestHeaders)) {
43  curl_setopt($ch, CURLOPT_HTTPHEADER, $message->requestHeaders);
44  } else {
45  curl_setopt($ch, CURLOPT_HEADER, 0);
46  }
47  if ($message->getMethod() === 'POST') {
48  curl_setopt($ch, CURLOPT_POST, true);
49  curl_setopt($ch, CURLOPT_POSTFIELDS, $message->request);
50  } elseif ($message->getMethod() !== 'GET') {
51  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $message->getMethod());
52  if (!is_null($message->request)) {
53  curl_setopt($ch, CURLOPT_POSTFIELDS, $message->request);
54  }
55  }
56  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
57  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
58  curl_setopt($ch, CURLOPT_HEADER, true);
59  $chResp = curl_exec($ch);
60  $message->requestHeaders = trim(str_replace("\r\n", "\n", curl_getinfo($ch, CURLINFO_HEADER_OUT)));
61  $chResp = str_replace("\r\n", "\n", $chResp);
62  $chRespSplit = explode("\n\n", $chResp, 2);
63  if ((count($chRespSplit) > 1) && (substr($chRespSplit[1], 0, 5) === 'HTTP/')) {
64  $chRespSplit = explode("\n\n", $chRespSplit[1], 2);
65  }
66  $message->responseHeaders = trim($chRespSplit[0]);
67  if (count($chRespSplit) > 1) {
68  $message->response = $chRespSplit[1];
69  } else {
70  $message->response = '';
71  }
72  $message->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
73  $message->ok = ($message->status >= 100) && ($message->status < 400);
74  if (!$message->ok) {
75  $message->error = curl_error($ch);
76  }
77  curl_close($ch);
78 
79  return $message->ok;
80  }
81 }
send(HttpMessage $message)
Send the request to the target URL.
Definition: CurlClient.php:37
Class to represent an HTTP message request.
Definition: HttpMessage.php:30
Class to implement the HTTP message interface using the Curl library.
Definition: CurlClient.php:28
getMethod()
Get the HTTP method for the request.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$message
Definition: xapiexit.php:32
Interface to represent an HTTP message client.
getUrl()
Get the target URL for the request.