包含带页码的网址

时间:2016-03-23 13:15:14

标签: php json

我坚持使用我的json解码脚本。我有一个api,它有699页(目前)所以我将它转换为players.php?page =(页码),其中JSON解码为api,然后将其放入我的数据库。但是我想在api给我的所有699页,而不是24个结果,这是我的文件:

    <?php
$conn = mysqli_connect("localhost","u1715p547","L0yRM6pd","u1715p547_ps");
mysqli_set_charset($conn,"utf8");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


    $page_data_url = "https://www.easports.com/fifa/ultimate-team/api/fut/item?page=1";

    // Get the JSON file from EASports
    $page_data_plain = file_get_contents($page_data_url);

    // Decode the JSON file to a PHP array
    $page_data_json = json_decode($page_data_plain, true);

    // Get the total pages count
    $total_pages_count = $page_data_json['totalPages'];

    // Loop through each page
    for ($page = 1; $page <= $total_pages_count; $page++){

        // Get the EASports JSON per specific page
        $item_url = 'https://www.easports.com/fifa/ultimate-team/api/fut/item?page='.$page;

        // Get the JSON file from EASports
        $item_data_plain = file_get_contents($item_url);

        // Decode the JSON file to a PHP array
        $item_data_json = json_decode($item_data_plain, true);

        // Count the amount of items
        $total_items_count = $item_data_json['count'];

        // Loop through all items, extract the values and insert in DB
        for ($c = 0; $c < $total_items_count; $c++) {

            $commonname00 = $item_data_json[$c]['commonName'];
            $commonname = str_replace("'", "''", $commonname00);
            $firstname00 = $item_data_json[$c]['firstName'];
            $firstname = str_replace("'", "''", $firstname00);
            $lastname00 = $item_data_json[$c]['lastName'];
            $lastname = str_replace("'", "''", $lastname00);
            $playerimg = $item_data_json[$c]['headshotImgUrl'];
            $leagueid = $item_data_json[$c]['league']['id'];
            $nationsmall = $item_data_json[$c]['nation']['imageUrls']['small'];
            $nationnormal = $item_data_json[$c]['nation']['imageUrls']['medium'];
            $nationlarge = $item_data_json[$c]['nation']['imageUrls']['large'];
            $nationid = $item_data_json[$c]['nation']['id'];
            $clubsmall = $item_data_json[$c]['club']['imageUrls']['normal']['small'];
            $clubnormal = $item_data_json[$c]['club']['imageUrls']['normal']['medium'];
            $clublarge = $item_data_json[$c]['club']['imageUrls']['normal']['large'];
            $clubid = $item_data_json[$c]['club']['id'];
            $largeImgUrl = $item_data_json[$c]['headshot']['largeImgUrl'];
            $medImgUrl = $item_data_json[$c]['headshot']['medImgUrl'];
            $smallImgUrl = $item_data_json[$c]['headshot']['smallImgUrl'];
            $largeSpecImgUrl = $item_data_json[$c]['specialImages']['largeTOTWImgUrl'];
            $medSpecImgUrl = $item_data_json[$c]['specialImages']['medTOTWImgUrl'];
            $pos = $item_data_json[$c]['position'];
            $ps = $item_data_json[$c]['playStyle'];
            $heig = $item_data_json[$c]['height'];
            $weig = $item_data_json[$c]['weight'];
            $bd = $item_data_json[$c]['birthdate'];
            $age = $item_data_json[$c]['age'];
            $acc = $item_data_json[$c]['acceleration'];
            $agg = $item_data_json[$c]['aggression'];
            $agi = $item_data_json[$c]['agility'];
            $bal = $item_data_json[$c]['balance'];
            $ball = $item_data_json[$c]['ballcontrol'];
            $foot = $item_data_json[$c]['foot'];
            $sm = $item_data_json[$c]['skillMoves'];
            $cro = $item_data_json[$c]['crossing'];
            $cur = $item_data_json[$c]['curve'];
            $dri = $item_data_json[$c]['dribbling'];
            $fin = $item_data_json[$c]['finishing'];
            $fca = $item_data_json[$c]['freekickaccuracy'];
            $gkd = $item_data_json[$c]['gkdiving'];
            $gkh = $item_data_json[$c]['gkhandling'];
            $gkk = $item_data_json[$c]['gkkicking'];
            $gkp = $item_data_json[$c]['gkpositioning'];
            $gkr = $item_data_json[$c]['gkreflexes'];
            $hea = $item_data_json[$c]['headingaccuracy'];
            $int = $item_data_json[$c]['interceptions'];
            $jum = $item_data_json[$c]['jumping'];
            $lop = $item_data_json[$c]['longpassing'];
            $los = $item_data_json[$c]['longshots'];
            $mar = $item_data_json[$c]['marking'];
            $pen = $item_data_json[$c]['penalties'];
            $poi = $item_data_json[$c]['positioning'];
            $pot = $item_data_json[$c]['potential'];
            $rea = $item_data_json[$c]['reactions'];
            $shp = $item_data_json[$c]['shortpassing'];
            $sho = $item_data_json[$c]['shotpower'];
            $slt = $item_data_json[$c]['slidingtackle'];
            $spr = $item_data_json[$c]['sprintspeed'];
            $stt = $item_data_json[$c]['standingtackle'];
            $sta = $item_data_json[$c]['stamina'];
            $str = $item_data_json[$c]['strength'];
            $vis = $item_data_json[$c]['vision'];
            $vol = $item_data_json[$c]['volleys'];
            $wf = $item_data_json[$c]['weakFoot'];
            $traits = $item_data_json[$c]['traits'][$c];
            $traits1 = $item_data_json[$c]['traits']['1'];
            $traits2 = $item_data_json[$c]['traits']['2'];
            $traits3 = $item_data_json[$c]['traits']['3'];
            $traits4 = $item_data_json[$c]['traits']['4'];
            $specialities = $item_data_json[$c]['specialities'][$c];
            $specialities1 = $item_data_json[$c]['specialities']['1'];
            $specialities2 = $item_data_json[$c]['specialities']['2'];
            $specialities3 = $item_data_json[$c]['specialities']['3'];
            $specialities4 = $item_data_json[$c]['specialities']['4'];
            $specialities5 = $item_data_json[$c]['specialities']['5'];
            $specialities6 = $item_data_json[$c]['specialities']['6'];
            $specialities7 = $item_data_json[$c]['specialities']['7'];
            $specialities8 = $item_data_json[$c]['specialities']['8'];
            $specialities9 = $item_data_json[$c]['specialities']['9'];
            $specialities10 = $item_data_json[$c]['specialities']['10'];
            $atk = $item_data_json[$c]['atkWorkRate'];
            $def = $item_data_json[$c]['defWorkRate'];
            $pty = $item_data_json[$c]['playerType'];
            $pace = $item_data_json[$c]['attributes'][$c]['value'];
            $shot = $item_data_json[$c]['attributes']['1']['value'];
            $pass = $item_data_json[$c]['attributes']['2']['value']; 
            $drib = $item_data_json[$c]['attributes']['3']['value'];
            $deff = $item_data_json[$c]['attributes']['4']['value'];
            $phys = $item_data_json[$c]['attributes']['5']['value'];
            $nameof00 = $item_data_json[$c]['name'];
            $nameof = str_replace("'", "''", $nameof00);
            $qua = $item_data_json[$c]['quality'];
            $color = $item_data_json[$c]['color'];
            $GK = $item_data_json[$c]['isGK'];
            $posfull = $item_data_json[$c]['positionFull'];
            $price = $item_data_json[$c]['discardValue'];
            $id = $item_data_json[$c]['id'];
            $baseId = $item_data_json[$c]['baseId'];
            $rating = $item_data_json[$c]['rating'];

    $sql = "INSERT IGNORE INTO `players`(`commonName`, `firstName`, `headshotImgUrl`, `lastName`, `leagueid`, `nationimageUrlssmall`, `nationimageUrlsmedium`, `nationimageUrlslarge`, `nationid`, `clubimageUrlsnormalsmall`, `clubimageUrlsnormalmedium`, `clubimageUrlsnormallarge`, `clubid`, `headshotlargeImgUrl`, `headshotmedImgUrl`, `headshotsmallImgUrl`, `specialImageslargeTOTWImgUrl`, `specialImagesmedTOTWImgUrl`, `position`, `playStyle`, `height`, `weight`, `birthdate`, `age`, `acceleration`, `aggression`, `agility`, `balance`, `ballcontrol`, `foot`, `skillMoves`, `crossing`, `curve`, `dribbling`, `finishing`, `freekickaccuracy`, `gkdiving`, `gkhandling`, `gkkicking`, `gkpositioning`, `gkreflexes`, `headingaccuracy`, `interceptions`, `jumping`, `longpassing`, `longshots`, `marking`, `penalties`, `positioning`, `potential`, `reactions`, `shortpassing`, `shotpower`, `slidingtackle`, `sprintspeed`, `standingtackle`, `stamina`, `strength`, `vision`, `volleys`, `weakFoot`, `traits0`, `traits1`, `traits2`, `traits3`, `specialities0`, `specialities1`, `specialities2`, `specialities3`, `specialities4`, `specialities5`, `specialities6`, `specialities7`, `specialities8`, `atkWorkRate`, `defWorkRate`, `playerType`, `attributes0value`, `attributes1value`, `attributes2value`, `attributes3value`, `attributes4value`, `attributes5value`, `name`, `quality`, `color`, `isGK`, `positionFull`, `discardValue`, `id`, `baseId`, `rating`, `specialities9`, `specialities10`, `traits4`)
    VALUES ('$commonname', '$firstname', '$playerimg', '$lastname', $leagueid, '$nationsmall', '$nationnormal', '$nationlarge',
    $nationid, '$clubsmall', '$clubnormal', '$clublarge', $clubid, '$largeImgUrl', '$medImgUrl',
    '$smallImgUrl', '$largeSpecImgUrl', '$medSpecImgUrl', '$pos', '$ps', $heig, $weig, '$bd', $age, $acc, $agg, $agi, $bal, $ball, '$foot', $sm, $cro, $cur, $dri,
    $fin, $fca, $gkd, $gkh, $gkk, $gkp, $gkr, $hea, $int, $jum, $lop, $los, $mar, $pen, $poi, $pot, $rea, $shp, $sho, $slt, $spr, $stt, $sta, $str, $vis, $vol, $wf,
    '$traits', '$traits1', '$traits2', '$traits3', '$specialities', '$specialities1', '$specialities2', '$specialities3', '$specialities4', '$specialities5',
    '$specialities6', '$specialities7', '$specialities8', '$atk', '$def', '$pty', $pace, $shot, $pass, $drib, $deff, $phys, '$nameof', '$qua', '$color', '$GK',
    '$posfull', '$price', $id, $baseId, $rating, '$specialities9', '$specialities10', '$traits4')";
    echo $sql;
    if(!$result = $conn->query($sql))
    {
        die("<script type='text/javascript'>alert(Fault);</script>");
    }
        }
    }
?>

我希望有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

<?php
    $page_data_url = "https://www.easports.com/fifa/ultimate-team/api/fut/item?page=1";

    // Get the JSON file from EASports
    $page_data_plain = file_get_contents($page_data_url);

    // Decode the JSON file to a PHP array
    $page_data_json = json_decode($page_data_plain, true);

    // Get the total pages count
    $total_pages_count = $page_data_json['totalPages'];

    // Loop through each page
    for ($page = 1; $page <= $total_pages_count; $page++){

        // Get the EASports JSON per specific page
        $item_url = 'https://www.easports.com/fifa/ultimate-team/api/fut/item?page=1'.$page;

        // Get the JSON file from EASports
        $item_data_plain = file_get_contents($item_url);

        // Decode the JSON file to a PHP array
        $item_data_json = json_decode($item_data_plain, true);

        // Count the amount of items
        $total_items_count = $item_data_json['count'];

        // Loop through all items, extract the values and insert in DB
        for ($c = 0; $c < $total_items_count; $c++) {

            $commonname00 = $item_data_json[$c]['commonName'];
            $commonname = str_replace("'", "''", $commonname00);
            $firstname00 = $item_data_json[$c]['firstName'];
            $firstname = str_replace("'", "''", $firstname00);
            $lastname00 = $item_data_json[$c]['lastName'];
            $lastname = str_replace("'", "''", $lastname00);
            $playerimg = $item_data_json[$c]['headshotImgUrl'];
            $leagueid = $item_data_json[$c]['league']['id'];
            $nationsmall = $item_data_json[$c]['nation']['imageUrls']['small'];
            $nationnormal = $item_data_json[$c]['nation']['imageUrls']['medium'];
            $nationlarge = $item_data_json[$c]['nation']['imageUrls']['large'];
            $nationid = $item_data_json[$c]['nation']['id'];
            $clubsmall = $item_data_json[$c]['club']['imageUrls']['normal']['small'];
            $clubnormal = $item_data_json[$c]['club']['imageUrls']['normal']['medium'];
            $clublarge = $item_data_json[$c]['club']['imageUrls']['normal']['large'];
            $clubid = $item_data_json[$c]['club']['id'];
            $largeImgUrl = $item_data_json[$c]['headshot']['largeImgUrl'];
            $medImgUrl = $item_data_json[$c]['headshot']['medImgUrl'];
            $smallImgUrl = $item_data_json[$c]['headshot']['smallImgUrl'];
            $largeSpecImgUrl = $item_data_json[$c]['specialImages']['largeTOTWImgUrl'];
            $medSpecImgUrl = $item_data_json[$c]['specialImages']['medTOTWImgUrl'];
            $pos = $item_data_json[$c]['position'];
            $ps = $item_data_json[$c]['playStyle'];
            $heig = $item_data_json[$c]['height'];
            $weig = $item_data_json[$c]['weight'];
            $bd = $item_data_json[$c]['birthdate'];
            $age = $item_data_json[$c]['age'];
            $acc = $item_data_json[$c]['acceleration'];
            $agg = $item_data_json[$c]['aggression'];
            $agi = $item_data_json[$c]['agility'];
            $bal = $item_data_json[$c]['balance'];
            $ball = $item_data_json[$c]['ballcontrol'];
            $foot = $item_data_json[$c]['foot'];
            $sm = $item_data_json[$c]['skillMoves'];
            $cro = $item_data_json[$c]['crossing'];
            $cur = $item_data_json[$c]['curve'];
            $dri = $item_data_json[$c]['dribbling'];
            $fin = $item_data_json[$c]['finishing'];
            $fca = $item_data_json[$c]['freekickaccuracy'];
            $gkd = $item_data_json[$c]['gkdiving'];
            $gkh = $item_data_json[$c]['gkhandling'];
            $gkk = $item_data_json[$c]['gkkicking'];
            $gkp = $item_data_json[$c]['gkpositioning'];
            $gkr = $item_data_json[$c]['gkreflexes'];
            $hea = $item_data_json[$c]['headingaccuracy'];
            $int = $item_data_json[$c]['interceptions'];
            $jum = $item_data_json[$c]['jumping'];
            $lop = $item_data_json[$c]['longpassing'];
            $los = $item_data_json[$c]['longshots'];
            $mar = $item_data_json[$c]['marking'];
            $pen = $item_data_json[$c]['penalties'];
            $poi = $item_data_json[$c]['positioning'];
            $pot = $item_data_json[$c]['potential'];
            $rea = $item_data_json[$c]['reactions'];
            $shp = $item_data_json[$c]['shortpassing'];
            $sho = $item_data_json[$c]['shotpower'];
            $slt = $item_data_json[$c]['slidingtackle'];
            $spr = $item_data_json[$c]['sprintspeed'];
            $stt = $item_data_json[$c]['standingtackle'];
            $sta = $item_data_json[$c]['stamina'];
            $str = $item_data_json[$c]['strength'];
            $vis = $item_data_json[$c]['vision'];
            $vol = $item_data_json[$c]['volleys'];
            $wf = $item_data_json[$c]['weakFoot'];
            $traits = $item_data_json[$c]['traits'][$c];
            $traits1 = $item_data_json[$c]['traits']['1'];
            $traits2 = $item_data_json[$c]['traits']['2'];
            $traits3 = $item_data_json[$c]['traits']['3'];
            $traits4 = $item_data_json[$c]['traits']['4'];
            $specialities = $item_data_json[$c]['specialities'][$c];
            $specialities1 = $item_data_json[$c]['specialities']['1'];
            $specialities2 = $item_data_json[$c]['specialities']['2'];
            $specialities3 = $item_data_json[$c]['specialities']['3'];
            $specialities4 = $item_data_json[$c]['specialities']['4'];
            $specialities5 = $item_data_json[$c]['specialities']['5'];
            $specialities6 = $item_data_json[$c]['specialities']['6'];
            $specialities7 = $item_data_json[$c]['specialities']['7'];
            $specialities8 = $item_data_json[$c]['specialities']['8'];
            $specialities9 = $item_data_json[$c]['specialities']['9'];
            $specialities10 = $item_data_json[$c]['specialities']['10'];
            $atk = $item_data_json[$c]['atkWorkRate'];
            $def = $item_data_json[$c]['defWorkRate'];
            $pty = $item_data_json[$c]['playerType'];
            $pace = $item_data_json[$c]['attributes'][$c]['value'];
            $shot = $item_data_json[$c]['attributes']['1']['value'];
            $pass = $item_data_json[$c]['attributes']['2']['value']; 
            $drib = $item_data_json[$c]['attributes']['3']['value'];
            $deff = $item_data_json[$c]['attributes']['4']['value'];
            $phys = $item_data_json[$c]['attributes']['5']['value'];
            $nameof00 = $item_data_json[$c]['name'];
            $nameof = str_replace("'", "''", $nameof00);
            $qua = $item_data_json[$c]['quality'];
            $color = $item_data_json[$c]['color'];
            $GK = $item_data_json[$c]['isGK'];
            $posfull = $item_data_json[$c]['positionFull'];
            $price = $item_data_json[$c]['discardValue'];
            $id = $item_data_json[$c]['id'];
            $baseId = $item_data_json[$c]['baseId'];
            $rating = $item_data_json[$c]['rating'];

            // INSERT THE OBJECT HERE IN THE DATABASE
        }
    }

?>