httphelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC;
  24. use OCP\Http\Client\IClientService;
  25. use OCP\IConfig;
  26. /**
  27. * Class HTTPHelper
  28. *
  29. * @package OC
  30. * @deprecated Use \OCP\Http\Client\IClientService
  31. */
  32. class HTTPHelper {
  33. const USER_AGENT = 'ownCloud Server Crawler';
  34. /** @var \OCP\IConfig */
  35. private $config;
  36. /** @var IClientService */
  37. private $clientService;
  38. /**
  39. * @param IConfig $config
  40. * @param IClientService $clientService
  41. */
  42. public function __construct(IConfig $config,
  43. IClientService $clientService) {
  44. $this->config = $config;
  45. $this->clientService = $clientService;
  46. }
  47. /**
  48. * Get URL content
  49. * @param string $url Url to get content
  50. * @throws \Exception If the URL does not start with http:// or https://
  51. * @return string of the response or false on error
  52. * This function get the content of a page via curl, if curl is enabled.
  53. * If not, file_get_contents is used.
  54. * @deprecated Use \OCP\Http\Client\IClientService
  55. */
  56. public function getUrlContent($url) {
  57. try {
  58. $client = $this->clientService->newClient();
  59. $response = $client->get($url);
  60. return $response->getBody();
  61. } catch (\Exception $e) {
  62. return false;
  63. }
  64. }
  65. /**
  66. * Returns the response headers of a HTTP URL without following redirects
  67. * @param string $location Needs to be a HTTPS or HTTP URL
  68. * @return array
  69. * @deprecated Use \OCP\Http\Client\IClientService
  70. */
  71. public function getHeaders($location) {
  72. $client = $this->clientService->newClient();
  73. $response = $client->get($location);
  74. return $response->getHeaders();
  75. }
  76. /**
  77. * Checks whether the supplied URL begins with HTTPS:// or HTTP:// (case insensitive)
  78. * @param string $url
  79. * @return bool
  80. */
  81. public function isHTTPURL($url) {
  82. return stripos($url, 'https://') === 0 || stripos($url, 'http://') === 0;
  83. }
  84. /**
  85. * send http post request
  86. *
  87. * @param string $url
  88. * @param array $fields data send by the request
  89. * @return array
  90. * @deprecated Use \OCP\Http\Client\IClientService
  91. */
  92. public function post($url, array $fields) {
  93. $client = $this->clientService->newClient();
  94. try {
  95. $response = $client->post($url, ['body' => $fields]);
  96. } catch (\Exception $e) {
  97. return ['success' => false, 'result' => $e->getMessage()];
  98. }
  99. return ['success' => true, 'result' => $response->getBody()];
  100. }
  101. }