PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / WebRequestClient.php

WebRequestClient.php in Packeta trunk, at src/Packetery/Module/WebRequestClient.php

62 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class WebRequestClient
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module;
11
12 use Packetery\Core\Api\WebRequestException;
13 use Packetery\Core\Interfaces\IWebRequestClient;
14
15 /**
16 * Class WebRequestClient
17 *
18 * @package Packetery
19 */
20 class WebRequestClient implements IWebRequestClient {
21
22 private const TIMEOUT = 30;
23
24 /**
25 * POST.
26 *
27 * @param string $url Target URL.
28 * @param array<string, mixed> $options Options.
29 *
30 * @throws WebRequestException WebRequestException.
31 */
32 public function post( string $url, array $options ): string {
33 $resultResponse = wp_remote_post( $url, $options );
34 if ( is_wp_error( $resultResponse ) ) {
35 throw new WebRequestException( $resultResponse->get_error_message() );
36 }
37
38 return wp_remote_retrieve_body( $resultResponse );
39 }
40
41 /**
42 * GET.
43 *
44 * @param string $url Target URL.
45 * @param array<string, mixed> $options Options.
46 *
47 * @return string
48 * @throws WebRequestException WebRequestException.
49 */
50 public function get( string $url, array $options = [] ): string {
51 $options[] = [
52 'timeout' => self::TIMEOUT,
53 ];
54 $resultResponse = wp_remote_get( $url, $options );
55 if ( is_wp_error( $resultResponse ) ) {
56 throw new WebRequestException( $resultResponse->get_error_message() );
57 }
58
59 return wp_remote_retrieve_body( $resultResponse );
60 }
61 }
62