Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Web;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   6  use PhpOffice\PhpSpreadsheet\Settings;
   7  use Psr\Http\Client\ClientExceptionInterface;
   8  
   9  class Service
  10  {
  11      /**
  12       * WEBSERVICE.
  13       *
  14       * Returns data from a web service on the Internet or Intranet.
  15       *
  16       * Excel Function:
  17       *        Webservice(url)
  18       *
  19       * @return string the output resulting from a call to the webservice
  20       */
  21      public static function webService(string $url)
  22      {
  23          $url = trim($url);
  24          if (strlen($url) > 2048) {
  25              return Functions::VALUE(); // Invalid URL length
  26          }
  27  
  28          if (!preg_match('/^http[s]?:\/\//', $url)) {
  29              return Functions::VALUE(); // Invalid protocol
  30          }
  31  
  32          // Get results from the the webservice
  33          $client = Settings::getHttpClient();
  34          $requestFactory = Settings::getRequestFactory();
  35          $request = $requestFactory->createRequest('GET', $url);
  36  
  37          try {
  38              $response = $client->sendRequest($request);
  39          } catch (ClientExceptionInterface $e) {
  40              return Functions::VALUE(); // cURL error
  41          }
  42  
  43          if ($response->getStatusCode() != 200) {
  44              return Functions::VALUE(); // cURL error
  45          }
  46  
  47          $output = $response->getBody()->getContents();
  48          if (strlen($output) > 32767) {
  49              return Functions::VALUE(); // Output not a string or too long
  50          }
  51  
  52          return $output;
  53      }
  54  
  55      /**
  56       * URLENCODE.
  57       *
  58       * Returns data from a web service on the Internet or Intranet.
  59       *
  60       * Excel Function:
  61       *        urlEncode(text)
  62       *
  63       * @param mixed $text
  64       *
  65       * @return string the url encoded output
  66       */
  67      public static function urlEncode($text)
  68      {
  69          if (!is_string($text)) {
  70              return Functions::VALUE();
  71          }
  72  
  73          return str_replace('+', '%20', urlencode($text));
  74      }
  75  }