Blame view

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