-
Notifications
You must be signed in to change notification settings - Fork 0
/
Request.php
executable file
·73 lines (60 loc) · 2.12 KB
/
Request.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
<?php
/**
* Author: Onur KAYA
* E-mail: empatisoft@gmail.com
* Date: 2021-11-25 10:00
*/
namespace Empatisoft\Api\Youtube;
use GuzzleHttp\Client;
class Request {
/**
* @param string $url
* @param array $headers
* @param bool $json
* @return mixed|string
*/
public function getRequest(string $url, array $headers = [], bool $json = true) {
try {
$client = new Client();
$parameters = [];
if(!empty($headers))
$parameters['headers'] = $headers;
$response = $client->request('GET', $url, $parameters)->getBody()->getContents();
if(!empty($response) && $json == true) {
$decode = json_decode($response, true);
if(json_last_error() === JSON_ERROR_NONE)
$response = $decode;
}
$result = $response;
} catch (\GuzzleHttp\Exception\GuzzleException $exception) {
$result = ['success' => false, 'code' => $exception->getCode(), 'message' => $exception->getMessage()];
}
return $result;
}
/**
* @param string $url
* @param array $data
* @param string $type (form_params veya json)
* @param array $headers
* @return array
*/
public function postRequest(string $url, array $data, string $type = 'form_params', array $headers = []):array {
try {
$result = [];
$client = new Client();
$parameters = [];
if(!empty($headers))
$parameters['headers'] = $headers;
$parameters[$type] = $data;
$response = $client->request('POST', $url, $parameters)->getBody()->getContents();
if(!empty($response)) {
$response = json_decode($response, true);
if(json_last_error() === JSON_ERROR_NONE)
$result = $response;
}
} catch (\GuzzleHttp\Exception\GuzzleException $exception) {
$result = ['success' => false, 'code' => $exception->getCode(), 'message' => $exception->getMessage()];
}
return $result;
}
}