Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Web;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
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 ExcelError::VALUE(); // Invalid URL length
26
        }
27
 
28
        if (!preg_match('/^http[s]?:\/\//', $url)) {
29
            return ExcelError::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 ExcelError::VALUE(); // cURL error
41
        }
42
 
43
        if ($response->getStatusCode() != 200) {
44
            return ExcelError::VALUE(); // cURL error
45
        }
46
 
47
        $output = $response->getBody()->getContents();
48
        if (strlen($output) > 32767) {
49
            return ExcelError::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 ExcelError::VALUE();
71
        }
72
 
73
        return str_replace('+', '%20', urlencode($text));
74
    }
75
}