Skip to content

Commit

Permalink
Change GET and POST calls to use CURL
Browse files Browse the repository at this point in the history
  • Loading branch information
jusasiiv committed Jul 26, 2018
1 parent fb42f66 commit e8928b7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
9 changes: 3 additions & 6 deletions blockonomics-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,22 +282,19 @@ private function checkForErrors($responseObj, $woocommerce) {
if(!isset($responseObj->response_code)) {
$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');

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

} else {

switch ($responseObj->response_code) {

case 'HTTP/1.1 200 OK':
case 200:
break;

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

case 'HTTP/1.1 500 Internal Server Error': {
case 500: {

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

Expand Down
49 changes: 28 additions & 21 deletions php/Blockonomics.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,42 @@ public function __construct()
{
}


public function new_address($api_key, $secret)
{
$options = array(
'http' => array(
'header' => 'Authorization: Bearer ' . $api_key,
'method' => 'POST',
'content' => '',
'ignore_errors' => true
)
);

$context = stream_context_create($options);
$contents = file_get_contents(Blockonomics::NEW_ADDRESS_URL."?match_callback=$secret", false, $context);
$responseObj = json_decode($contents);

//Create response object if it does not exist
if (!isset($responseObj)) $responseObj = new stdClass();
$responseObj->{'response_code'} = $http_response_header[0];
$url = Blockonomics::NEW_ADDRESS_URL . "?match_callback=" . $secret;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $api_key,
'Content-type: application/x-www-form-urlencoded'
));
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$responseObj = json_decode($data);
if (!isset($responseObj)) {
$responseObj = new stdClass();
}
$responseObj->{'response_code'} = $httpcode;
return $responseObj;
}

public function get_price($currency)
{
$options = array( 'http' => array( 'method' => 'GET') );
$context = stream_context_create($options);
$contents = file_get_contents(Blockonomics::PRICE_URL. "?currency=$currency", false, $context);
$price = json_decode($contents);
$url = Blockonomics::PRICE_URL. "?currency=$currency";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$data = curl_exec($ch);
curl_close($ch);
$price = json_decode($data);
return $price->price;
}
}

0 comments on commit e8928b7

Please sign in to comment.