从json_url获取数据以及如何插入到我的MySQL数据库

时间:2020-09-26 08:03:48

标签: php arrays json

这是从 $ api_url

获取的json数据

我想将这些数据插入到我的mysql数据库中,获取json数据后应该做什么?

我尝试了一些教程,但是没有用,因为我听不懂

救救我。

Array
([match_id] => 41962
    [title] => Australia Women vs India Women
    [competition] => Array
        ([title] => ICC Women's T20 World Cup
            [abbr] => iwtwc-201920
            [type] => tournament
            [category] => international
            [match_format] => woment20)
[teama] => Array
        ( [team_id] => 8652
            [name] => Australia Women
            [short_name] => AUS-W
            [logo_url] => https://cricket.entitysport.com/assets/uploads/2016/03/australia-women.png
            [scores_full] => 184/4 (20 ov)  )
    [teamb] => Array
        ( [team_id] => 9536
            [name] => India Women
            [short_name] => IND-W
            [logo_url] => https://cricket.entitysport.com/assets/uploads/2016/03/india-women.png
            [scores_full] => *99/10 (19.1 ov)
    [date_start] => 2020-03-08 07:00:00
    [date_end] => 2020-03-08 19:00:00
    [venue] => Array
        ( [venue_id] => 111
            [name] => Melbourne Cricket Ground
            [location] => Melbourne)
    [umpires] => Ahsan Raza (Pakistan), Kim Cotton (New Zealand), Gregory Brathwaite (West Indies, TV)
    [referee] => Chris Broad (England)
    [toss] => Array
        ( [text] => Australia Women won the toss & elected to bat
            [winner] => 8652
            [decision] => 1))
Array
(" " second data's)
to be continue ...

我只是在这里获取json数据 mtach.php


$api_url='https://rest.entitysport.com/v2/matches/?status=2&token=ec471071441bb2ac538a0ff901abd249';

$ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL,$api_url);
        $result=curl_exec($ch);
        curl_close($ch);
        $cricketMatches= json_decode(json_encode(json_decode($result)), True);
            
           foreach($cricketMatches as $matchs) { 
            foreach ($matchs as $items) {
            foreach ($items as $item) {
                echo "<pre>"; print_r($item);
//code for insert data to mysql.
            }
            }
           }

谢谢

1 个答案:

答案 0 :(得分:0)

这应该做到。

<?php

$api_url = 'https://rest.entitysport.com/v2/matches/?status=2&token=ec471071441bb2ac538a0ff901abd249';

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $api_url);
$result = curl_exec($ch);
curl_close($ch);
$result_json = json_decode(json_encode(json_decode($result)), True);
$data = $result_json["response"];

// setup PDO connection 
$pdo = new PDO('mysql:host=localhost;dbname=cricket', 'root', '');
$stmt = $pdo->prepare(
        "INSERT INTO `cricket`
                (match_id, competition_id, toss_winner_team, winner_team, teamid1, teamid2, match_status, matchStarted,
                type, time, match_date_time, title, team1Score, team1Over, team2Score, team2Over, match_status_note)
        VALUES
                (:match_id, :competition_id, :toss_winner_team, :winner_team, :teamid1, :teamid2, :match_status, :matchStarted,
                :type, :time, :match_date_time, :title, :team1Score, :team1Over, :team2Score, :team2Over, :match_status_note)
        "
);
try {
        $pdo->beginTransaction();
        foreach ($data["items"] as $key => $value) {
                $stmt->bindParam(':match_id', $value['match_id']); 
                $stmt->bindParam(':competition_id', $value['competition']['cid']); 

                $stmt->bindParam(':toss_winner_team', $value['toss']['text']); 
                $stmt->bindParam(':winner_team', $value['winning_team_id']); 
                $stmt->bindParam(':teamid1', $value['teama']['team_id']); 
                $stmt->bindParam(':teamid2', $value['teamb']['team_id']); 

                $stmt->bindParam(':match_status', $value['status_str']); 
                $stmt->bindParam(':matchStarted', $value['date_start']); 
                $stmt->bindParam(':type', $value['competition']['type']); 
                $stmt->bindParam(':time', $value['timestamp_start']); 
                $stmt->bindParam(':match_date_time', $value['date_end']); 
                $stmt->bindParam(':title', $value['title']); 

                $stmt->bindParam(':team1Score', $value['teama']['scores_full']); 
                $stmt->bindParam(':team1Over', $value['teama']['overs']); 
                $stmt->bindParam(':team2Score', $value['teamb']['scores_full']); 
                $stmt->bindParam(':team2Over', $value['teamb']['overs']); 

                $stmt->bindParam(':match_status_note', $value['status_note']); 

                $stmt->execute();

                // echo "Inserted {$key} item..<br>";
        }
        $pdo->commit();
} catch (Exception $e) {
        $pdo->rollback();
        throw $e;
}