Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
6 commits by dmzoneill
Browse files Browse the repository at this point in the history
Automatically fix CA certificate issues in certain WAMP installations
Several improvements + 6 commits by dmzoneill
  • Loading branch information
Jon Eyrick authored May 3, 2018
2 parents 9f0786e + 53e048a commit 269e1bc
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/constructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

require '../php-binance-api.php';

$api_key = "gW3TsYrpPoSBEX61rvGfor00ATivkbXNp3ODnNhmQpQjv4nfl8LnGXOX1iL9LcHa";
$api_secret = "bqNINND57jzM8wDYVi6AWRjE9uzF4Q6BJ0V0FvJzdLUmq7c0uhvwUugeW67hx8Bm";

$api1 = new Binance\API();
$balances1 = $api1->balances();
print count($balances1) . "\n";

$api2 = new Binance\API( "/home/dave/.config/jaggedsoft/php-binance-api.json" );
$balances2 = $api2->balances();
print count($balances2) . "\n";

$api3 = new Binance\API($api_key, $api_secret);
$balances3 = $api3->balances();
print count($balances3) . "\n";



6 changes: 6 additions & 0 deletions examples/report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

require 'php-binance-api.php';

$api = new Binance\API();
$api->report();
142 changes: 142 additions & 0 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public function __construct()
$this->api_key = $param[0];
$this->api_secret = $param[1];
break;
default:
echo 'Please see valid constructors here: https://github.com/jaggedsoft/php-binance-api/blob/master/examples/constructor.php';
}
}

Expand Down Expand Up @@ -821,6 +823,10 @@ private function httpRequest(string $url, string $method = "GET", array $params
throw new \Exception("Sorry cURL is not installed!");
}

if (file_exists(getcwd() . '/ca.pem') === false) {
$this->downloadCurlCaBundle();
}

$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, $this->httpDebug);
$query = http_build_query($params, '', '&');
Expand Down Expand Up @@ -889,6 +895,10 @@ private function httpRequest(string $url, string $method = "GET", array $params
curl_setopt($curl, constant($key), $value);
}

if (file_exists(getcwd() . '/ca.pem')) {
curl_setopt($curl, CURLOPT_CAINFO, getcwd() . '/ca.pem');
}

$output = curl_exec($curl);
// Check if any error occurred
if (curl_errno($curl) > 0) {
Expand Down Expand Up @@ -2082,4 +2092,136 @@ public function miniTicker(callable $callback)
});
// @codeCoverageIgnoreEnd
}

/**
* Due to ongoing issues with out of date wamp CA bundles
* This function downloads ca bundle for curl website
* and uses it as part of the curl options
*/
private function downloadCurlCaBundle()
{
$output_filename = getcwd() . "/ca.pem";

$host = "https://curl.haxx.se/ca/cacert.pem";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $host);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

// proxy settings
if (is_array($this->proxyConf)) {
curl_setopt($curl, CURLOPT_PROXY, $this->getProxyUriString());
if (isset($this->proxyConf['user']) && isset($this->proxyConf['pass'])) {
curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->proxyConf['user'] . ':' . $this->proxyConf['pass']);
}
}

$result = curl_exec($curl);
curl_close($curl);

$fp = fopen($output_filename, 'w');
fwrite($fp, $result);
fclose($fp);
}

/**
* Report
*/
public function report()
{
$phpversion = phpversion();
$curlversion = phpversion('curl');
$dns1 = dns_get_record("api.binance.com", DNS_ANY, $authns, $addtl);
$dns2 = dns_get_record($dns1[0]['target'], DNS_ANY, $authns, $addtl);
$uname = php_uname();
$platform = PHP_OS;
$composer_installed = shell_exec("composer show 2>&1");

$fp = @fsockopen("api.binance.com", 443, $errno, $errstr, 0.1);
$api_access = false;

if (!$fp) {
$api_access = false;
} else {
fclose($fp);
$api_access = true;
}

$fp = @fsockopen("stream.binance.com", 9443, $errno, $errstr, 0.1);
$stream_access = false;

if (!$fp) {
$stream_access = false;
} else {
fclose($fp);
$stream_access = true;
}

$this->downloadCurlCaBundle();

$out = fopen('php://output', 'w');
ob_start();

$host = "https://api.binance.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_STDERR, $out);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
$result = curl_exec($ch);
curl_close($ch);

fclose($out);
$with_system_ca = ob_get_clean();

$out = fopen('php://output', 'w');
ob_start();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_STDERR, $out);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '/ca.pem');
curl_setopt($ch, CURLOPT_CAPATH, '/dev/null');
$result = curl_exec($ch);
curl_close($ch);

fclose($out);
$with_downloaded_ca = ob_get_clean();

$output = "## Uname: " . PHP_EOL;
$output .= " - " . $uname . PHP_EOL;
$output .= "## Platform: " . PHP_EOL;
$output .= " - " . $platform . PHP_EOL;
$output .= "## PHP Version: " . PHP_EOL;
$output .= " - " . $phpversion . PHP_EOL;
$output .= "## PHP Curl Version: " . PHP_EOL;
$output .= " - " . $curlversion . PHP_EOL;
$output .= "## DNS : " . PHP_EOL;
$output .= "```" . PHP_EOL . print_r($dns1, true) . PHP_EOL . "```" . PHP_EOL;
$output .= "## DNS Extra: " . PHP_EOL;
$output .= "```" . PHP_EOL . print_r($dns2, true) . PHP_EOL . "```" . PHP_EOL;
$output .= "## Curl Using System CA: " . PHP_EOL;
$output .= "```" . PHP_EOL . $with_system_ca . PHP_EOL . "```" . PHP_EOL;
$output .= "## Curl Using Downloaded CA: " . PHP_EOL;
$output .= "```" . PHP_EOL . $with_downloaded_ca . PHP_EOL . "```" . PHP_EOL;
$output .= "## Port Access 443 api.binance.com: " . PHP_EOL;
$output .= " - " . ($api_access ? "open" : "blocked") . PHP_EOL;
$output .= "## Port Access 9443 stream.binance.com: " . PHP_EOL;
$output .= " - " . ($stream_access ? "open" : "blocked") . PHP_EOL;
$output .= "## Composer modules: " . PHP_EOL;
$output .= "```" . PHP_EOL . $composer_installed . PHP_EOL . "```" . PHP_EOL;

file_put_contents("debug.txt", $output);
}
}

0 comments on commit 269e1bc

Please sign in to comment.