Completed
Push — master ( ef471c...6dc74c )
by François
02:06
created

Http::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Bouncer package.
5
 *
6
 * (c) François Hodierne <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bouncer;
13
14
class Http
15
{
16
17
    public static function request($method, $url, $data = null, $timeout = 2)
18
    {
19
        $options = array(
20
            'http' => array(
21
                'timeout' => $timeout,
22
                'method'  => $method,
23
                'header'  => "User-Agent: Bouncer Http\r\n"
24
            )
25
        );
26
        if ($data) {
27
            $content = json_encode($data);
28
            $length = strlen($content);
29
            $options['http']['header'] .= "Content-Type: application/json\r\n";
30
            $options['http']['header'] .= "Content-Length: {$length}\r\n";
31
            $options['http']['content'] = $content;
32
        }
33
        $context = stream_context_create($options);
34
        $result = @file_get_contents($url, false, $context);
35
        if ($result) {
36
            $response = json_decode($result, true);
37
            return $response;
38
        }
39
    }
40
41
    public static function get($url, $data = null, $timeout = 2)
42
    {
43
        return self::request('GET', $url, $data, $timeout);
44
    }
45
46
}
47