/** * Returns transactions for given address ordered from the oldest. * * @param IAddress $address * @return array of Transaction */ public function getTransactions(IAddress $address) { if (isset($this->addressTransactions[$address->getId()])) { return $this->addressTransactions[$address->getId()]; } // construct first page url $url = static::ADDRESS_API_URL . $address->getId() . '?limit=' . static::ADDRESS_API_LIMIT; // fetch the page $request = new Request($url); $request->setCertificationVerify(FALSE); $response = $request->get()->getResponse(); // parse the response $data = json_decode($response, TRUE); $page = 0; // loop over all pages $transactions = array(); while (count($data['txs']) > 0) { foreach ($data['txs'] as $tx) { if (isset($tx['block_height'])) { // include only confirmed transactions $transaction = $this->getTransaction($tx['hash']); if (!in_array($transaction, $transactions)) { $transactions[] = $this->getTransaction($tx['hash']); } } } // if there is only one page, end the loop during the first run if ($page === 0 && $data['n_tx'] <= static::ADDRESS_API_LIMIT) { break; } // count next offset $page++; $offset = $page * static::ADDRESS_API_LIMIT - $page * static::ADDRESS_API_BUFFER; $nextUrl = "{$url}&offset={$offset}"; // fetch the page $request = new Request($nextUrl); $request->setCertificationVerify(FALSE); $response = $request->get()->getResponse(); // parse the response $data = json_decode($response, TRUE); } // the oldest first $this->addressTransactions[$address->getId()] = array_reverse($transactions); // return result return $this->addressTransactions[$address->getId()]; }
/** * @param array $data * @throws CommunicationFailedException * @return array */ private function process($data) { $this->onRequest($data); $data = array('USER' => $this->username, 'PWD' => $this->password, 'SIGNATURE' => $this->signature, 'VERSION' => self::API_VERSION) + $data; $request = new Curl\Request($this->host, $data); $request->setSender($this->curlSender); $request->options['verbose'] = TRUE; if (strpos($request->getUrl()->getHost(), '.sandbox.') !== FALSE) { $request->setCertificationVerify(FALSE); $request->options['ssl_verifyHost'] = FALSE; } try { $response = $request->post(http_build_query($data)); $resultData = self::parseNvp($response->getResponse()); $this->onSuccess($resultData, $response->getInfo()); return $resultData; } catch (Curl\FailedRequestException $e) { $this->onError($e, $e->getInfo()); throw new CommunicationFailedException($e->getMessage(), 0, $e); } catch (Curl\CurlException $e) { $this->onError($e, $e->getResponse() ? $e->getResponse()->info : array()); throw new CommunicationFailedException($e->getMessage(), 0, $e); } }
/** * Returns TRUE if address is a valid bitcoin address. * * @param string $address * @return bool */ public function isValidAddress($address) { // construct first page url $url = static::ADDRESS_API_URL . $address . '?limit=1'; // fetch the page $request = new Request($url); $request->setCertificationVerify(FALSE); try { $request->get()->getResponse(); return TRUE; } catch (BadStatusException $e) { return FALSE; } }