Skip to content

Commit e8928b7

Browse files
committed
Change GET and POST calls to use CURL
1 parent fb42f66 commit e8928b7

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

blockonomics-woocommerce.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,22 +282,19 @@ private function checkForErrors($responseObj, $woocommerce) {
282282
if(!isset($responseObj->response_code)) {
283283
$error_str = __('Your webhost is blocking outgoing HTTPS connections. Blockonomics requires an outgoing HTTPS POST (port 443) to generate new address. Check with your webhosting provider to allow this.', 'blockonomics-bitcoin-payments');
284284

285-
} elseif(!ini_get('allow_url_fopen')) {
286-
$error_str = __('The allow_url_fopen is not enabled, please enable this option to allow address generation.', 'blockonomics-bitcoin-payments');
287-
288285
} else {
289286

290287
switch ($responseObj->response_code) {
291288

292-
case 'HTTP/1.1 200 OK':
289+
case 200:
293290
break;
294291

295-
case 'HTTP/1.1 401 Unauthorized': {
292+
case 401: {
296293
$error_str = __('API Key is incorrect. Make sure that the API key set in admin Blockonomics module configuration is correct.', 'blockonomics-bitcoin-payments');
297294
break;
298295
}
299296

300-
case 'HTTP/1.1 500 Internal Server Error': {
297+
case 500: {
301298

302299
if(isset($responseObj->message)) {
303300

php/Blockonomics.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,42 @@ public function __construct()
1010
{
1111
}
1212

13-
1413
public function new_address($api_key, $secret)
1514
{
16-
$options = array(
17-
'http' => array(
18-
'header' => 'Authorization: Bearer ' . $api_key,
19-
'method' => 'POST',
20-
'content' => '',
21-
'ignore_errors' => true
22-
)
23-
);
24-
25-
$context = stream_context_create($options);
26-
$contents = file_get_contents(Blockonomics::NEW_ADDRESS_URL."?match_callback=$secret", false, $context);
27-
$responseObj = json_decode($contents);
28-
29-
//Create response object if it does not exist
30-
if (!isset($responseObj)) $responseObj = new stdClass();
31-
$responseObj->{'response_code'} = $http_response_header[0];
15+
$url = Blockonomics::NEW_ADDRESS_URL . "?match_callback=" . $secret;
3216

17+
$ch = curl_init();
18+
curl_setopt($ch, CURLOPT_URL, $url);
19+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
20+
curl_setopt($ch, CURLOPT_POST, 1);
21+
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
22+
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
23+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
24+
'Authorization: Bearer ' . $api_key,
25+
'Content-type: application/x-www-form-urlencoded'
26+
));
27+
$data = curl_exec($ch);
28+
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
29+
curl_close($ch);
30+
$responseObj = json_decode($data);
31+
if (!isset($responseObj)) {
32+
$responseObj = new stdClass();
33+
}
34+
$responseObj->{'response_code'} = $httpcode;
3335
return $responseObj;
3436
}
3537

3638
public function get_price($currency)
3739
{
38-
$options = array( 'http' => array( 'method' => 'GET') );
39-
$context = stream_context_create($options);
40-
$contents = file_get_contents(Blockonomics::PRICE_URL. "?currency=$currency", false, $context);
41-
$price = json_decode($contents);
40+
$url = Blockonomics::PRICE_URL. "?currency=$currency";
41+
42+
$ch = curl_init();
43+
curl_setopt($ch, CURLOPT_URL, $url);
44+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
45+
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
46+
$data = curl_exec($ch);
47+
curl_close($ch);
48+
$price = json_decode($data);
4249
return $price->price;
4350
}
4451
}

0 commit comments

Comments
 (0)