连接两段jSON数据

时间:2018-09-10 19:05:33

标签: php json

我正在尝试从https://bushell.net/betfair.php中检索数据,然后为“ section_id”(这是一匹独特的马)实施正确的价格。我使用唯一的市场ID密钥,并访问另一个URL

例如https://uk-api.betfair.com/www/sports/exchange/readonly/v1.0/bymarket?currencyCode=GBP&alt=json&locale=en_GB&types=MARKET_STATE%2CRUNNER_STATE%2CRUNNER_EXCHANGE_PRICES_BEST&marketIds=1.147791452

(market_id是一个变量,您可以从https://bushell.net/betfair.php中获得)

enter image description here

然后使用回声将其吐出。

从逻辑上讲,我的代码在某些地方无法正常工作?

<?php

echo date("Y-m-d h:i:s");

echo "<br>";



ini_set('max_execution_time', 30000);



$curl = curl_init();



curl_setopt_array($curl, array(

    CURLOPT_URL => "https://bushell.net/betfair.php",

    CURLOPT_RETURNTRANSFER => true,

    CURLOPT_ENCODING => "",

    CURLOPT_MAXREDIRS => 10,

    CURLOPT_TIMEOUT => 3000000,

    CURLOPT_SSL_VERIFYPEER => false

    ));



$response = curl_exec($curl);

$err = curl_error($curl);



curl_close($curl);



if ($err) 

{

    echo "cURL Error #:" . $err;

} 

else

{

    $master_data = array();

    $price_data = array();

    $size_data = array();



    $marketIds = "";



    $response = json_decode($response,true);

    foreach ($response as $key => $value) 

    {

        $name = "";

        $d = null;

        $d1 = null;



        $marketIds .= $value['marketId'].",";



        foreach ($value['runners'] as $keyInner => $valueInner) 

        {

            $d = explode("T", $value['marketStartTime']);

            $d1 = explode(".", $d[1]);

            $curl1 = curl_init();



            $name = $value['event']['venue'];



            $temp = array();

            $temp['image'] = "<img src='https://content-cache.cdnbf.net/feeds_images/Horses/SilkColours/".$valueInner['metadata']['COLOURS_FILENAME']."'>";

            $temp['runnerName'] = $valueInner['runnerName'];

            $temp['jockey'] = $valueInner['metadata']['JOCKEY_NAME'];

            $temp['venue'] = $value['event']['venue']."-".$value['marketName'];

            $temp['dateTime'] = date('Y-m-d H:i:s', strtotime($value['marketStartTime']));

            $temp['odds'] = 0.0;

            $temp['selectionId'] = $valueInner['selectionId'];

            $master_data[] = $temp;

        }


          //New api end - market id comma

    $curl2 = curl_init();
    curl_setopt_array($curl2, array(
      CURLOPT_URL => "https://uk-api.betfair.com/www/sports/exchange/readonly/v1.0/bymarket?currencyCode=GBP&alt=json&locale=en_GB&types=MARKET_STATE%2CRUNNER_STATE%2CRUNNER_EXCHANGE_PRICES_BEST&marketIds=".$marketIds,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30000,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_SSL_VERIFYPEER => false
    ));


    $response2 = curl_exec($curl2);
    $err2 = curl_error($curl2);
    curl_close($curl2);

    if ($err2) 

    {

      echo "cURL Error #:" . $err2;

    }

    else 

    {

        $odds_array = json_decode($response2,true);

        foreach ($odds_array['eventTypes'][0]['eventNodes'] as $key2 => $value2) 

        {

            foreach ($value2['marketNodes'][0]['runners'] as $key_runner => $value_runner) 

            {

                $price_data[$value_runner['selectionId']] = @$value_runner['exchange']['availableToBack'][0]['price'];

                $size_data[$value_runner['selectionId']] = @$value_runner['exchange']['availableToBack'][0]['size'];  

            };

        }

        //die;

    }




    foreach ($master_data as $key => $value) 

    {
            $back_odds = @$price_data[$value['selectionId']];

            echo $value['image'];
            echo $value['runnerName']."<br>";
            echo "<strong>Jockey: </strong>". $value['jockey']."<br>";
            echo "<strong>Venue: </strong>".$value['venue']."<br>";
            echo "<strong>Date/Time: </strong>".$value['dateTime']."<br>";
            echo "<strong>Betfair Back Odds: </strong>".$back_odds."<br>";
            echo "<hr>";
    }

}

echo "<br>";

        ?>

1 个答案:

答案 0 :(得分:0)

运行PHP代码时,您可以告诉我一些错误吗?

因此,我运行了您的PHP代码,并看到一个错误,我通过添加未关闭“ if”语句的“}”来解决了该问题。

<?php
 echo date("Y-m-d h:i:s");
 echo "<br>";
 ini_set('max_execution_time', 30000);
 $curl = curl_init();
 curl_setopt_array($curl, array(
CURLOPT_URL => "https://bushell.net/betfair.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 3000000,
CURLOPT_SSL_VERIFYPEER => false
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

if ($err){
   echo "cURL Error #:" . $err;
}else
{
$master_data = array();
$price_data = array();
$size_data = array();
$marketIds = "";
$response = json_decode($response,true);

foreach ($response as $key => $value){
    $name = "";
    $d = null;
    $d1 = null;
    $marketIds .= $value['marketId'].",";
    foreach ($value['runners'] as $keyInner => $valueInner){
        $d = explode("T", $value['marketStartTime']);
        $d1 = explode(".", $d[1]);
        $curl1 = curl_init();
        $name = $value['event']['venue'];
        $temp = array();
        $temp['image'] = "<img src='https://content-cache.cdnbf.net/feeds_images/Horses/SilkColours/".$valueInner['metadata']['COLOURS_FILENAME']."'>";
        $temp['runnerName'] = $valueInner['runnerName'];
        $temp['jockey'] = $valueInner['metadata']['JOCKEY_NAME'];
        $temp['venue'] = $value['event']['venue']."-".$value['marketName'];
        $temp['dateTime'] = date('Y-m-d H:i:s', strtotime($value['marketStartTime']));
        $temp['odds'] = 0.0;
        $temp['selectionId'] = $valueInner['selectionId'];
        $master_data[] = $temp;
    }


    //New api end - market id comma
    $curl2 = curl_init();
    curl_setopt_array($curl2, array(
        CURLOPT_URL => "https://uk-api.betfair.com/www/sports/exchange/readonly/v1.0/bymarket?currencyCode=GBP&alt=json&locale=en_GB&types=MARKET_STATE%2CRUNNER_STATE%2CRUNNER_EXCHANGE_PRICES_BEST&marketIds=".$marketIds,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30000,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_SSL_VERIFYPEER => false
    ));

        $response2 = curl_exec($curl2);
        $err2 = curl_error($curl2);
        curl_close($curl2);

    if ($err2){
        echo "cURL Error #:" . $err2;
    }else{
        $odds_array = json_decode($response2,true);
        foreach ($odds_array['eventTypes'][0]['eventNodes'] as $key2 => $value2){
            foreach ($value2['marketNodes'][0]['runners'] as $key_runner => $value_runner){
                $price_data[$value_runner['selectionId']] = @$value_runner['exchange']['availableToBack'][0]['price'];
                $size_data[$value_runner['selectionId']] = @$value_runner['exchange']['availableToBack'][0]['size'];
            };
        }
        //die;
    }

    foreach ($master_data as $key => $value){
        $back_odds = @$price_data[$value['selectionId']];
        echo $value['image'];
        echo $value['runnerName']."<br>";
        echo "<strong>Jockey: </strong>". $value['jockey']."<br>";
        echo "<strong>Venue: </strong>".$value['venue']."<br>";
        echo "<strong>Date/Time: </strong>".$value['dateTime']."<br>";
        echo "<strong>Betfair Back Odds: </strong>".$back_odds."<br>";
        echo "<hr>";
    }
}
}
echo "<br>";
?>

请让我知道它是否可以修复您的代码。