Парсер курсов с tradingview.com

Последнее обновление: 3 октября, 2020

parsing

На выходе получаем такое:

Доллар:
EUR/USD - 1.17216 (ask = 1.17225, bid = 1.17204, open = 1.17454, close = 1.17216)

Фунт стерлингов:
EUR/GBP - 0.90605 (ask = 0.90628, bid = 0.90611, open = 0.91129, close = 0.90605)

Швейцарский франк:
EUR/CHF - 1.07912 (ask = 1.07924, bid = 1.07909, open = 1.07832, close = 1.07912)

Биткоин:
BTC/EUR - 8954.11 (ask = 8954, bid = 8948.62, open = 9046.18, close = 8954.11)

Эфириум:
ETH/EUR - 289.88 (ask = 290, bid = 289.65, open = 300.06, close = 289.88)

Непосредственно код:

<?php
    $res = info("EURUSD");
    echo "<p>Доллар:<br/>EUR/USD - {$res["rate"]} (ask = {$res["ask"]}, bid = {$res["bid"]}, open = {$res["open"]}, close = {$res["close"]})</p>";

    sleep(1);

    $res = info("EURGBP");
    echo "<p>Фунт стерлингов: <br/>EUR/GBP - {$res["rate"]} (ask = {$res["ask"]}, bid = {$res["bid"]}, open = {$res["open"]}, close = {$res["close"]})</p>";

    sleep(1);	

    $res = info("EURCHF");
    echo "<p>Швейцарский франк: <br/>EUR/CHF - {$res["rate"]} (ask = {$res["ask"]}, bid = {$res["bid"]}, open = {$res["open"]}, close = {$res["close"]})</p>";

    sleep(1);

    $res = info("BTCEUR", "BITSTAMP");
    echo "<p>Биткоин:<br/>BTC/EUR - {$res["rate"]} (ask = {$res["ask"]}, bid = {$res["bid"]}, open = {$res["open"]}, close = {$res["close"]})</p>";

    sleep(1);

    $res = info("ETHEUR", "BITSTAMP");
    echo "<p>Эфириум:<br/>ETH/EUR - {$res["rate"]} (ask = {$res["ask"]}, bid = {$res["bid"]}, open = {$res["open"]}, close = {$res["close"]})</p>";

function info( $ticker, $exchange = "FX_IDC" ) {

    $path = str_ireplace(basename(__FILE__),'',__FILE__);
    $coockie=$path."cookie.txt";
    $userAgent='Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3';
    $url = 'https://scanner.tradingview.com/forex/scan';

    $params = [
            "symbols" => ["tickers" => ["{$exchange}:{$ticker}"],"query" => ["types" => [	"forex"] ]],
            "columns" => ["close", "open", "bid", "ask"]
    ];


    if ( $exchange != "FX_IDC" ) {
            $params["symbols"]["query"]["types"] = [];
            $url = 'https://scanner.tradingview.com/crypto/scan';
    };

    $ch = curl_init( $url ); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_REFERER, 'https://scanner.tradingview.com/'); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $coockie);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $coockie);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    $text = curl_exec($ch); 

    $data = json_decode($text, true);
    $res = [];
    foreach ( $params["columns"] as $index => $column ) {
            $res[ $column ] = $data["data"][0]["d"][$index];
    };

    //$delta = $res["ask"] - $res["bid"];
    //$res["rate"]  = $res["bid"] + ($res["ask"] - $res["bid"]) / 2;
    $res["rate"]  = $res["close"];

    curl_close($ch); 		

    return $res;

}