Completed
Push — master ( 1b0622...b59bdb )
by François
01:52
created

SimpleClient::post()   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 2
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\Http;
13
14
class SimpleClient
15
{
16
17
    protected $timeout = 2;
18
19
    public function request($method, $url, $data = null)
20
    {
21
        $options = array(
22
            'http' => array(
23
                'timeout' => $this->timeout,
24
                'method'  => $method,
25
                'header'  => "User-Agent: Bouncer Http\r\n"
26
            )
27
        );
28
        if ($data) {
29
            $content = json_encode($data);
30
            $length = strlen($content);
31
            $options['http']['header'] .= "Content-Type: application/json\r\n";
32
            $options['http']['header'] .= "Content-Length: {$length}\r\n";
33
            $options['http']['content'] = $content;
34
        }
35
        $context = stream_context_create($options);
36
        $result = @file_get_contents($url, false, $context);
37
        if ($result) {
38
            $response = json_decode($result, true);
39
            return $response;
40
        }
41
    }
42
43
    public function get($url, $data = null)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
        return self::request('GET', $url);
46
    }
47
48
    public function post($url, $data = null)
49
    {
50
        return self::request('POST', $url, $data);
51
    }
52
53
}
54