Last active
April 28, 2023 08:37
-
-
Save mattiasghodsian/a516aaa5c05d5a58037dd17f30f1b15d to your computer and use it in GitHub Desktop.
Fetch crypto exchanges with coins they offer from coinmarketcap.com (Don't use for live sites, this is experimental and for practise)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Title: coinmarketcap exchanges | |
| * Author: Mattias Ghodsian | |
| * Description: Fetch Coin exchanges with coins they offer (Don't use for live sites, this is experimental and for practise) | |
| * Site: https://coinmarketcap.com/exchanges/volume/24-hour/all/ | |
| * Donate a cup of coffee: https://www.buymeacoffee.com/mattiasghodsian | |
| * Donate Eth: 0xBBB96204E45D11C9799c6B12E6eE6F0d4A071Ef5 | |
| **/ | |
| function getExchangeData($exchanges, $coins){ | |
| $data = ['meta' => ['source' => 'coinmarketcap.com', 'datatype' => 'exchanges', 'interval' => '24h', 'version' => 'alpha (practise)', 'created' => '9/05/2018', 'developer' => 'Mattias Ghodsian'] ]; | |
| $url = 'https://coinmarketcap.com/exchanges/volume/24-hour/all/'; | |
| libxml_use_internal_errors(true); | |
| $doc = new DOMDocument(); | |
| $doc->loadHTMLFile($url); | |
| $xpath = new DOMXpath($doc); | |
| $elements = $xpath->query("//table"); | |
| if (!is_null($elements)) { | |
| foreach ($elements as $key => $element) { | |
| $sort = preg_replace('/[0-9]+. /', '#split#', $element->nodeValue); // make it easy to split the string | |
| $sort = str_replace('#CurrencyPairVolume (24h)PriceVolume (%)', '', $sort); // remove unwanted data | |
| $sort = explode('#split#', $sort); // split the data with our temp keyword | |
| unset($sort[0]); // remove unwanted data | |
| foreach ($sort as $key => $row) { | |
| $currencieList = []; | |
| $row = str_replace(PHP_EOL, ' ', $row); // replace \n with space | |
| $splitRow = explode(' ', $row); // split data to array | |
| $row = str_replace([$splitRow[0], ' 1 '], '', $row); // remove exchange name | |
| $currencies = explode('% ', $row); // split currencies | |
| unset( $currencies[array_pop($currencies)] ); // remove last array | |
| foreach ($currencies as $currency) { | |
| $currency = preg_replace('/^([0-9]* \w+ )?(.*)$/', '$2', $currency); // remove number and space start of string | |
| $currency = explode(' ', $currency); | |
| $name = strtolower($currency[0]); | |
| $currencieList[] = $currency; | |
| } | |
| $data[strtolower( $splitRow[0] )] = [ | |
| 'name' => $splitRow[0], | |
| 'currencies' => $currencieList | |
| ]; | |
| } | |
| } | |
| return $data; | |
| }else{ | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.