1
|
|
|
<?php namespace SOSTheBlack\Moip; |
2
|
|
|
|
3
|
|
|
use App; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* MoIP's API connection class |
7
|
|
|
* |
8
|
|
|
* @author Herberth Amaral |
9
|
|
|
* @author Paulo Cesar |
10
|
|
|
* @version 0.0.2 |
11
|
|
|
* @license <a href="http://www.opensource.org/licenses/bsd-license.php">BSD License</a> |
12
|
|
|
*/ |
13
|
|
|
class Client { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Method send() |
17
|
|
|
* |
18
|
|
|
* Send the request to API's server |
19
|
|
|
* |
20
|
|
|
* @param string $credentials Token and key to the authentication |
21
|
|
|
* @param string $xml The XML request |
22
|
|
|
* @param string $url The server's URL |
23
|
|
|
* @param string $method Method used to send the request |
24
|
|
|
* @throws Exception |
25
|
|
|
* @return \SOSTheBlack\Moip\Response |
26
|
|
|
*/ |
27
|
|
|
public function send($credentials, $xml, $url='https://desenvolvedor.moip.com.br/sandbox/ws/alpha/EnviarInstrucao/Unica', $method='POST') { |
28
|
|
|
$header[] = "Authorization: Basic " . base64_encode($credentials); |
|
|
|
|
29
|
|
|
if (!function_exists('curl_init')){ |
30
|
|
|
throw new Exception('This library needs cURL extension'); |
31
|
|
|
} |
32
|
|
|
$curl = curl_init(); |
33
|
|
|
curl_setopt($curl, CURLOPT_URL, $url); |
34
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); |
35
|
|
|
curl_setopt($curl, CURLOPT_USERPWD, $credentials); |
36
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
37
|
|
|
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0"); |
38
|
|
|
|
39
|
|
|
if ($method == 'POST') curl_setopt($curl, CURLOPT_POST, true); |
40
|
|
|
|
41
|
|
|
if ($xml != '') curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); |
42
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
43
|
|
|
$ret = curl_exec($curl); |
44
|
|
|
$err = curl_error($curl); |
45
|
|
|
curl_close($curl); |
46
|
|
|
|
47
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['resposta' => $ret, 'erro' => $err]]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $credentials token / key authentication Moip |
52
|
|
|
* @param string $xml url request |
53
|
|
|
* @param string $url url request |
54
|
|
|
* @param string $error errors |
55
|
|
|
* @return \SOSTheBlack\Moip\Response |
56
|
|
|
*/ |
57
|
|
|
function curlPost($credentials, $xml, $url, $error=null) { |
|
|
|
|
58
|
|
|
|
59
|
|
|
if (!$error) { |
|
|
|
|
60
|
|
|
$header[] = "Expect:"; |
|
|
|
|
61
|
|
|
$header[] = "Authorization: Basic " . base64_encode($credentials); |
62
|
|
|
|
63
|
|
|
$ch = curl_init(); |
64
|
|
|
$options = array(CURLOPT_URL => $url, |
65
|
|
|
CURLOPT_HTTPHEADER => $header, |
66
|
|
|
CURLOPT_SSL_VERIFYPEER => false, |
67
|
|
|
CURLOPT_POST => true, |
68
|
|
|
CURLOPT_POSTFIELDS => $xml, |
69
|
|
|
CURLOPT_RETURNTRANSFER => true, |
70
|
|
|
CURLINFO_HEADER_OUT => true |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
curl_setopt_array($ch, $options); |
74
|
|
|
$ret = curl_exec($ch); |
75
|
|
|
$err = curl_error($ch); |
76
|
|
|
$info = curl_getinfo($ch); |
77
|
|
|
curl_close($ch); |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
if ($info['http_code'] == "200") |
81
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['response' => true, 'error' => null, 'xml' => $ret]]); |
82
|
|
View Code Duplication |
elseif ($info['http_code'] == "500") |
|
|
|
|
83
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Error processing XML', 'xml' => null]]); |
84
|
|
View Code Duplication |
elseif ($info['http_code'] == "401") |
|
|
|
|
85
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Authentication failed', 'xml' => null]]); |
86
|
|
View Code Duplication |
else |
|
|
|
|
87
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $err, 'xml' => null]]); |
88
|
|
View Code Duplication |
} else { |
|
|
|
|
89
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $error, 'xml' => null]]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $credentials token / key authentication Moip |
96
|
|
|
* @param string $url url request |
97
|
|
|
* @param string $error errors |
98
|
|
|
* @return \SOSTheBlack\Moip\Response |
99
|
|
|
*/ |
100
|
|
|
function curlGet($credentials, $url, $error=null) { |
|
|
|
|
101
|
|
|
|
102
|
|
|
if (!$error) { |
|
|
|
|
103
|
|
|
$header[] = "Expect:"; |
|
|
|
|
104
|
|
|
$header[] = "Authorization: Basic " . base64_encode($credentials); |
105
|
|
|
|
106
|
|
|
$ch = curl_init(); |
107
|
|
|
$options = array(CURLOPT_URL => $url, |
108
|
|
|
CURLOPT_HTTPHEADER => $header, |
109
|
|
|
CURLOPT_SSL_VERIFYPEER => false, |
110
|
|
|
CURLOPT_RETURNTRANSFER => true |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
curl_setopt_array($ch, $options); |
114
|
|
|
$ret = curl_exec($ch); |
115
|
|
|
$err = curl_error($ch); |
116
|
|
|
$info = curl_getinfo($ch); |
117
|
|
|
curl_close($ch); |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
if ($info['http_code'] == "200") |
121
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['response' => true, 'error' => null, 'xml' => $ret]]); |
122
|
|
|
else if ($info['http_code'] == "500") |
123
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Error processing XML', 'xml' => null]]); |
124
|
|
|
else if ($info['http_code'] == "401") |
125
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Authentication failed', 'xml' => null]]); |
126
|
|
View Code Duplication |
else |
|
|
|
|
127
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $err, 'xml' => null]]); |
128
|
|
View Code Duplication |
} else { |
|
|
|
|
129
|
|
|
return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $error, 'xml' => null]]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.