├── src └── lib │ ├── Utils │ ├── ErrorHandler.php │ ├── ApiRequest.php │ └── WebSocket.php │ ├── Api.php │ └── Features │ └── Ticker.php ├── package.json ├── Sample.php ├── composer.json ├── LICENSE └── README.md /src/lib/Utils/ErrorHandler.php: -------------------------------------------------------------------------------- 1 | strtotime('+10 second', time()) // Required 7 | ]; 8 | 9 | try { 10 | 11 | // Initialization for our ticker 12 | $CDApi = new CoinDesk\Api(); 13 | 14 | // If you need only one tick 15 | $OneTick = $CDApi->ticker()->getOneTick(); 16 | echo "
" . json_encode($OneTick, JSON_PRETTY_PRINT) . ""; 17 | 18 | // Connect to the stream channel 19 | $CDApi->ticker()->setTicker(function (array $Coins) { 20 | echo "
" . json_encode($Coins, JSON_PRETTY_PRINT) . ""; 21 | }, $Settings); 22 | 23 | } catch (Exception $e) { 24 | 25 | // Throw out exceptions 26 | echo "Error: " . $e->getMessage(); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shahradelahi/coindesk-ticker", 3 | "description": "A PHP library to get realtime prices from CoinDesk.", 4 | "keywords": [ 5 | "coindesk", 6 | "price", 7 | "crypto", 8 | "ticker", 9 | "php" 10 | ], 11 | "type": "library", 12 | "homepage": "https://github.com/shahradelahi/coindesk-ticker", 13 | "license": "MIT", 14 | "support": { 15 | "issues": "https://github.com/shahradelahi/coindesk-ticker/issues", 16 | "source": "https://github.com/shahradelahi/coindesk-ticker" 17 | }, 18 | "authors": [ 19 | { 20 | "name": "Shahrad Elahi", 21 | "homepage": "https://github.com/shahradelahi" 22 | } 23 | ], 24 | "require": { 25 | "php": ">=8.0", 26 | "ext-openssl": "*", 27 | "ext-curl": "*" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "coindesk-ticker\\": "src/lib/" 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Shahrad Elahi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/lib/Api.php: -------------------------------------------------------------------------------- 1 | ApiRequest = $this->ApiRequest ?: new ApiRequest(); 37 | return $this->ApiRequest; 38 | } 39 | 40 | /** 41 | * @return Ticker 42 | */ 43 | public function ticker(): Ticker 44 | { 45 | $this->Ticker = $this->Ticker ?: new Ticker(); 46 | return $this->Ticker; 47 | } 48 | 49 | /** 50 | * It's just current price of Bitcoin in 3 different pairs. 51 | * Pairs: USD, EUR, GBP 52 | * 53 | * @return array 54 | */ 55 | public function getCurrentPrice(): array 56 | { 57 | return $this->getApiRequest()->sendRequest("bpi/currentprice.json"); 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /src/lib/Utils/ApiRequest.php: -------------------------------------------------------------------------------- 1 | 'application/json', 25 | 'Content-Type' => 'application/json', 26 | 'Pragma: ' => 'no-cache' 27 | ]; 28 | 29 | /** 30 | * @param string $endpoint 31 | * @param array $parameters 32 | * @return array 33 | */ 34 | public function sendRequest(string $endpoint, array $parameters = []): array 35 | { 36 | $queryString = http_build_query($parameters); // query string encode the parameters 37 | 38 | $endPointUrl = $this->apiPath . $endpoint . "?" . $queryString; // create the request URL 39 | 40 | $curl = curl_init(); // Get cURL resource 41 | 42 | // Set cURL options 43 | curl_setopt_array($curl, array( 44 | CURLOPT_URL => $endPointUrl, // set the request URL 45 | CURLOPT_HTTPHEADER => $this->headers, // set the headers 46 | CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool 47 | )); 48 | 49 | $response = curl_exec($curl); // Send the request, save the response 50 | curl_close($curl); // Close request 51 | 52 | return json_decode($response, true); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/lib/Features/Ticker.php: -------------------------------------------------------------------------------- 1 | WebSocket = new WebSocket(); 24 | } 25 | 26 | /** 27 | * @param array $settings 28 | */ 29 | private function Initialization(array $settings): void 30 | { 31 | ini_set('max_execution_time', '0'); 32 | $this->CloseTime = $settings['close_time'] ?: 0; 33 | } 34 | 35 | private function subscribeChannel(WebSocket $WebSocket, $Connection): void 36 | { 37 | usleep(500 * 1000); 38 | $WebSocket->websocket_write($Connection, '{"type":"hello","version":"2","id":1}'); 39 | usleep(500 * 1000); 40 | $WebSocket->websocket_write($Connection, '{"type":"sub","path":"/v2/ticker/all","id":2}'); 41 | usleep(500 * 1000); 42 | } 43 | 44 | /** 45 | * @return bool 46 | * @throws Exception 47 | */ 48 | private function makeConnection(): bool 49 | { 50 | if ($this->Connection = $this->WebSocket->websocket_open('production.api.coindesk.com', 443, '', $errstr, 10, true)) { 51 | 52 | // Starting for receiver 53 | return true; 54 | 55 | 56 | } else { 57 | throw new Exception("Connection Failed: $errstr"); 58 | } 59 | } 60 | 61 | /** 62 | * @param $Query 63 | * @param array $Settings 64 | * @throws Exception 65 | */ 66 | public function setTicker($Query, array $Settings): void 67 | { 68 | $this->Initialization($Settings); 69 | if ($this->makeConnection()) { 70 | 71 | // Subscribe to stream 72 | $this->subscribeChannel($this->WebSocket, $this->Connection); 73 | 74 | // Starting for receiver 75 | while ($this->CloseTime > time()) { 76 | $message = $this->WebSocket->websocket_read($this->Connection, $errstr); 77 | if ($message != "") { 78 | $JsonMessage = json_decode($message, true); 79 | if ($JsonMessage['type'] == "pub") { 80 | $Query($JsonMessage['message']); 81 | } 82 | } 83 | } 84 | 85 | } 86 | } 87 | 88 | /** 89 | * @throws Exception 90 | */ 91 | public function getOneTick(): array|bool 92 | { 93 | $oneTick = false; 94 | if ($this->makeConnection()) { 95 | 96 | // Subscribe to stream 97 | $this->subscribeChannel($this->WebSocket, $this->Connection); 98 | 99 | // Setup close timer 100 | $this->CloseTime = strtotime('+10 second', time()); 101 | 102 | // Starting for receiver 103 | while ($this->CloseTime > time()) { 104 | $message = $this->WebSocket->websocket_read($this->Connection, $errstr); 105 | if ($message != "") { 106 | $JsonMessage = json_decode($message, true); 107 | if ($JsonMessage['type'] == "pub") { 108 | 109 | $oneTick = $JsonMessage['message']; 110 | break; 111 | 112 | } 113 | } 114 | } 115 | 116 | } 117 | 118 | return $oneTick; 119 | } 120 | 121 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > **Help wanted:** This library is officially depreciated and will only be actively maintained by the community.
" . json_encode($Tick, JSON_PRETTY_PRINT) . ""; 76 | 77 | }, $Settings); 78 | ``` 79 | 80 |
" . json_encode($OneTick, JSON_PRETTY_PRINT) . ""; 133 | ``` 134 | 135 |
" . json_encode($CurrentPrice, JSON_PRETTY_PRINT) . ""; 190 | ``` 191 | 192 |