Skip to content

Commit 93d06b3

Browse files
authored
Update HttpSMS.php
1 parent d499ef9 commit 93d06b3

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

src/HttpSMS.php

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,84 +6,78 @@
66

77
class HttpSMS
88
{
9+
private $method;
10+
private $balanceUrl;
11+
private $balanceParams;
12+
private $smsUrl;
13+
private $smsParams;
14+
15+
public function __construct()
16+
{
17+
$this->method = config('smsconfig.method');
18+
$this->balanceUrl = config('smsconfig.balance_url');
19+
$this->balanceParams = config('smsconfig.balance_params');
20+
$this->smsUrl = config('smsconfig.sms_url');
21+
$this->smsParams = config('smsconfig.sms_params');
22+
}
923

1024
public function getBalance($userParams = [])
1125
{
12-
if (config('smsconfig.method') === 'get') {
13-
// Generate URL with (Query) from Params
14-
$url = config('smsconfig.balance_url') . '?';
15-
$params = config('smsconfig.balance_params');
16-
return $this->requestCurlGet($url, $params, $userParams);
17-
}
18-
if (config('smsconfig.method') === 'post') {
19-
$url = config('smsconfig.balance_url');
20-
$params = config('smsconfig.balance_params');
21-
return $this->requestCurlPost($url, $params, $userParams);
22-
}
23-
return "Please insert valid request method in config file (SMS CONFIG)";
26+
return $this->makeRequest($this->balanceUrl, $this->balanceParams, $userParams);
2427
}
2528

2629
public function sendMessage($userParams = [])
2730
{
28-
if (config('smsconfig.method') === 'get') {
29-
// Generate URL with (Query) from Params
30-
$url = config('smsconfig.sms_url') . '?';
31-
$params = config('smsconfig.sms_params');
31+
return $this->makeRequest($this->smsUrl, $this->smsParams, $userParams);
32+
}
33+
34+
private function makeRequest($url, $params, $userParams)
35+
{
36+
if ($this->method === 'get') {
3237
return $this->requestCurlGet($url, $params, $userParams);
33-
}
34-
if (config('smsconfig.method') === 'post') {
35-
// Generate URL with (Query) from Params
36-
$url = config('smsconfig.sms_url');
37-
$params = config('smsconfig.sms_params');
38+
} elseif ($this->method === 'post') {
3839
return $this->requestCurlPost($url, $params, $userParams);
3940
}
40-
return "Please insert valid request method in config file (SMS CONFIG)";
41+
return "Please insert a valid request method in the config file (SMS CONFIG)";
4142
}
4243

43-
4444
private function requestCurlGet($url, $params, $userParams)
4545
{
46-
// params from Config file
47-
if ($params != null)
48-
foreach ($params as $key => $value) {
49-
$url = $url . '&' . $key . '=' . $value;
50-
}
46+
$query = http_build_query(array_merge($params, $userParams));
47+
$finalUrl = rtrim($url, '?') . '?' . $query;
5148

52-
// user params
53-
if ($userParams != null)
54-
foreach ($userParams as $key => $value) {
55-
$url = $url . '&' . $key . '=' . $value;
56-
}
49+
$finalUrl = str_replace([" ", "\n"], ["%20", "%0A"], $finalUrl);
5750

58-
$url = preg_replace("/ /", "%20", $url);
59-
$url = preg_replace("/\n/", "%0A", $url);
60-
61-
//cURL HTTP GET
6251
$ch = curl_init();
63-
curl_setopt($ch, CURLOPT_URL, $url);
64-
curl_setopt($ch, CURLOPT_POST, 0);
52+
curl_setopt($ch, CURLOPT_URL, $finalUrl);
6553
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6654

6755
$response = curl_exec($ch);
68-
//$err = curl_error($ch); //if you need
56+
if ($response === false) {
57+
$error = curl_error($ch);
58+
curl_close($ch);
59+
return "Curl error: $error";
60+
}
6961
curl_close($ch);
7062
return $response;
7163
}
7264

7365
private function requestCurlPost($url, $params, $userParams)
7466
{
67+
$postFields = array_merge($params, $userParams);
7568

76-
$post = array_merge($params, $userParams);
77-
//cURL HTTP POST
7869
$ch = curl_init($url);
7970
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
80-
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
71+
curl_setopt($ch, CURLOPT_POST, true);
72+
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
8173

8274
$response = curl_exec($ch);
83-
// $err = curl_errno($ch);
84-
curl_close($ch); //if you need
85-
75+
if ($response === false) {
76+
$error = curl_error($ch);
77+
curl_close($ch);
78+
return "Curl error: $error";
79+
}
80+
curl_close($ch);
8681
return $response;
87-
8882
}
89-
}
83+
}

0 commit comments

Comments
 (0)