See Release Notes
Long Term Support Release
Differences Between: [Versions 400 and 401]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace enrol_lti\local\ltiadvantage\lib; 18 19 use Packback\Lti1p3\Interfaces\IHttpClient; 20 use Packback\Lti1p3\Interfaces\IHttpException; 21 use Packback\Lti1p3\Interfaces\IHttpResponse; 22 23 /** 24 * An implementation of IHTTPClient delegating to a curl object, for use with the lib/lti1p3 library code. 25 * 26 * @package enrol_lti 27 * @copyright 2022 Jake Dallimore <jrhdallimore@gmail.com> 28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 */ 30 class http_client implements IHttpClient { 31 32 /** @var \curl a curl client instance. */ 33 private $curlclient; 34 35 /** 36 * Constructor. 37 * 38 * @param \curl $curlclient a curl client instance. 39 */ 40 public function __construct(\curl $curlclient) { 41 $this->curlclient = $curlclient; 42 } 43 44 /** 45 * Make an HTTP request to the given URL. 46 * 47 * @param string $method the HTTP method to use. 48 * @param string $url the URL to send the request to. 49 * @param array $options an array of request options, mainly used to set headers and body. 50 * @return IHttpResponse the response 51 * @throws \Exception if the curl client encounters any errors making the request. 52 * @throws IHttpException if the response contains a 400-level or 500-level status code. 53 */ 54 public function request(string $method, string $url, array $options): IHttpResponse { 55 $this->curlclient->resetHeader(); 56 $this->curlclient->resetopt(); 57 if (isset($options['headers'])) { 58 $headers = $options['headers']; 59 array_walk( 60 $headers, 61 function(&$val, $key) { 62 $val = "$key: $val"; 63 } 64 ); 65 $this->curlclient->setHeader($headers); 66 } 67 if ($method == 'POST') { 68 $body = $options['body'] ?? null; 69 $body = $body ?? (!empty($options['form_params']) ? http_build_query($options['form_params'], '' , '&') : null); 70 $res = $this->curlclient->post($url, $body, ['CURLOPT_HEADER' => 1]); 71 } else if ($method == 'GET') { 72 $res = $this->curlclient->get($url, [], ['CURLOPT_HEADER' => 1]); 73 } else { 74 throw new \Exception('Sorry, that HTTP method is not supported yet.'); 75 } 76 77 $info = $this->curlclient->get_info(); 78 if (!$this->curlclient->get_errno() && !$this->curlclient->error) { 79 // No errors, so format the response. 80 $resbody = substr($res ?? '', $info['header_size']); 81 $headers = $this->curlclient->getResponse(); 82 $response = new http_response(['headers' => $headers, 'body' => $resbody], intval($info['http_code'])); 83 if ($response->getStatusCode() >= 400) { 84 throw new http_exception($response, "An HTTP error status was received: '".reset($headers)."'"); 85 } 86 return $response; 87 } 88 // The curl client experienced errors, so report that. 89 throw new \Exception("There was a cURL error when making the request: errno: {$this->curlclient->get_errno()}, 90 error: {$this->curlclient->error}."); 91 } 92 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body