【问题标题】:Use API JSON to display a URL for an HTML Page使用 API JSON 显示 HTML 页面的 URL
【发布时间】:2021-08-14 20:24:48
【问题描述】:

我对所有这些都是全新的,所以请原谅我描述和命名事物的任何问题。我有一个 HTML 页面,用户在其中输入 URL 字符串的一部分,以便使用完整的 URL 字符串启动一个新窗口。我宁愿只是使用 API 查找 URL 的一部分来创建 JSON 文件并自动找到它。有什么想法可以在没有用户干预的情况下完成此操作吗?

HTML(包含用于启动新窗口的文本框和按钮,我想完全绕过它)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
     <title>Cast Challonge</title>
 <script type="text/javascript" src="https://code.jquery.com/jquery-3.6.0.js"></script>

 <script type="text/javascript">

 $(document).ready(function(){

     $('#button').click(function(e) {  
         var inputvalue = $("#input").val();
         window.open(("http://challonge.com/"+inputvalue+"/module?theme=5928&selected-round- 
filter=2&show_final_results=0&multiplier=2&show_tournament_name=1&scale_to_fit=1"),"Tournament 
 Display","width=1280,height=720,resizable=no, toolbar=no, scrollbars=no, menubar=no, status=no, 
 directories=no");

     });
 });
 </script> 
 </head>
 <body>

        <input type="text" value="" id="input"> 
        <button type="button" id="button">Submit Tournament ID</button>
 </body>
 </html>

我的示例 API 调用如下:

[
  {
    "tournament": {
      "id": 9838153,
      "name": "2021/05/22 Saturday Night 8-Ball",
      "url": "BnkSat0522",
      "description": "",
      "tournament_type": "double elimination",
      "started_at": "2021-05-23T07:01:41.429+07:00",
      "completed_at": "2021-05-23T12:54:30.331+07:00",
      "require_score_agreement": false,
      "notify_users_when_matches_open": true,
      "created_at": "2021-05-23T03:41:49.015+07:00",
      "updated_at": "2021-05-23T12:54:30.580+07:00",
      "state": "complete",
      "open_signup": false,
      "notify_users_when_the_tournament_ends": true,
      "progress_meter": 100,
      "quick_advance": false,
      "hold_third_place_match": false,
      "pts_for_game_win": "0.0",
      "pts_for_game_tie": "0.0",
      "pts_for_match_win": "1.0",
      "pts_for_match_tie": "0.5",
      "pts_for_bye": "1.0",
      "swiss_rounds": 0,
      "private": false,
      "ranked_by": "match wins",
      "show_rounds": true,
      "hide_forum": false,
      "sequential_pairings": false,
      "accept_attachments": false,
      "rr_pts_for_game_win": "0.0",
      "rr_pts_for_game_tie": "0.0",
      "rr_pts_for_match_win": "1.0",
      "rr_pts_for_match_tie": "0.5",
      "created_by_api": false,
      "credit_capped": false,
      "category": null,
      "hide_seeds": false,
      "prediction_method": 0,
      "predictions_opened_at": null,
      "anonymous_voting": false,
      "max_predictions_per_user": 1,
      "signup_cap": null,
      "game_id": 773,
      "participants_count": 24,
      "group_stages_enabled": false,
      "allow_participant_match_reporting": false,
      "teams": false,
      "check_in_duration": null,
      "start_at": null,
      "started_checking_in_at": null,
      "tie_breaks": [
        "match wins vs tied",
        "game wins",
        "points scored"
      ],
      "locked_at": null,
      "event_id": null,
      "public_predictions_before_start_time": false,
      "ranked": false,
      "grand_finals_modifier": null,
      "predict_the_losers_bracket": false,
      "spam": null,
      "ham": null,
      "rr_iterations": 1,
      "tournament_registration_id": null,
      "donation_contest_enabled": null,
      "mandatory_donation": null,
      "non_elimination_tournament_data": {
        "participants_per_match": ""
      },
      "auto_assign_stations": null,
      "only_start_matches_with_stations": null,
      "registration_fee": "0.0",
      "registration_type": "free",
      "split_participants": false,
      "allowed_regions": [
        
      ],
      "show_participant_country": null,
      "program_id": null,
      "program_classification_ids_allowed": null,
      "team_size_range": null,
      "toxic": null,
      "description_source": "",
      "subdomain": null,
      "full_challonge_url": "https://challonge.com/BnkSat0522",
      "live_image_url": "https://challonge.com/BnkSat0522.svg",
      "sign_up_url": null,
      "review_before_finalizing": true,
      "accepting_predictions": false,
      "participants_locked": true,
      "game_name": "8-ball",
      "participants_swappable": false,
      "team_convertable": false,
      "group_stages_were_started": false
    }
  },
  {
    "tournament": {
      "id": 9845539,
      "name": "2021/05/24 Monday Night 10-Ball",
      "url": "20210524",
      "description": "",
      "tournament_type": "double elimination",
      "started_at": "2021-05-25T06:34:46.053+07:00",
      "completed_at": "2021-05-25T10:54:03.406+07:00",
      "require_score_agreement": false,
      "notify_users_when_matches_open": true,
      "created_at": "2021-05-25T04:01:56.922+07:00",
      "updated_at": "2021-05-25T10:54:03.589+07:00",
      "state": "complete",
      "open_signup": false,
      "notify_users_when_the_tournament_ends": true,
      "progress_meter": 100,
      "quick_advance": false,
      "hold_third_place_match": false,
      "pts_for_game_win": "0.0",
      "pts_for_game_tie": "0.0",
      "pts_for_match_win": "1.0",
      "pts_for_match_tie": "0.5",
      "pts_for_bye": "1.0",
      "swiss_rounds": 0,
      "private": false,
      "ranked_by": "match wins",
      "show_rounds": true,
      "hide_forum": false,
      "sequential_pairings": false,
      "accept_attachments": false,
      "rr_pts_for_game_win": "0.0",
      "rr_pts_for_game_tie": "0.0",
      "rr_pts_for_match_win": "1.0",
      "rr_pts_for_match_tie": "0.5",
      "created_by_api": false,
      "credit_capped": false,
      "category": null,
      "hide_seeds": false,
      "prediction_method": 0,
      "predictions_opened_at": null,
      "anonymous_voting": false,
      "max_predictions_per_user": 1,
      "signup_cap": null,
      "game_id": 6341,
      "participants_count": 22,
      "group_stages_enabled": false,
      "allow_participant_match_reporting": true,
      "teams": false,
      "check_in_duration": null,
      "start_at": null,
      "started_checking_in_at": null,
      "tie_breaks": [
        "match wins vs tied",
        "game wins",
        "points scored"
      ],
      "locked_at": null,
      "event_id": null,
      "public_predictions_before_start_time": false,
      "ranked": false,
      "grand_finals_modifier": null,
      "predict_the_losers_bracket": false,
      "spam": null,
      "ham": null,
      "rr_iterations": 1,
      "tournament_registration_id": null,
      "donation_contest_enabled": null,
      "mandatory_donation": null,
      "non_elimination_tournament_data": {
        "participants_per_match": ""
      },
      "auto_assign_stations": false,
      "only_start_matches_with_stations": false,
      "registration_fee": "0.0",
      "registration_type": "free",
      "split_participants": false,
      "allowed_regions": [
        
      ],
      "show_participant_country": null,
      "program_id": null,
      "program_classification_ids_allowed": null,
      "team_size_range": null,
      "toxic": null,
      "description_source": "",
      "subdomain": null,
      "full_challonge_url": "https://challonge.com/20210524",
      "live_image_url": "https://challonge.com/20210524.svg",
      "sign_up_url": null,
      "review_before_finalizing": true,
      "accepting_predictions": false,
      "participants_locked": true,
      "game_name": "10-Ball",
      "participants_swappable": false,
      "team_convertable": false,
      "group_stages_were_started": false
    }
  },
  {
    "tournament": {
      "id": 9851001,
      "name": "2021/05/26 Wednesday Night 9-Ball",
      "url": "20210526",
      "description": "",
      "tournament_type": "double elimination",
      "started_at": "2021-05-26T20:48:53.085+07:00",
      "completed_at": null,
      "require_score_agreement": false,
      "notify_users_when_matches_open": true,
      "created_at": "2021-05-26T20:46:47.342+07:00",
      "updated_at": "2021-05-26T20:48:53.158+07:00",
      "state": "underway",
      "open_signup": false,
      "notify_users_when_the_tournament_ends": true,
      "progress_meter": 0,
      "quick_advance": false,
      "hold_third_place_match": false,
      "pts_for_game_win": "0.0",
      "pts_for_game_tie": "0.0",
      "pts_for_match_win": "1.0",
      "pts_for_match_tie": "0.5",
      "pts_for_bye": "1.0",
      "swiss_rounds": 0,
      "private": false,
      "ranked_by": "match wins",
      "show_rounds": true,
      "hide_forum": true,
      "sequential_pairings": false,
      "accept_attachments": false,
      "rr_pts_for_game_win": "0.0",
      "rr_pts_for_game_tie": "0.0",
      "rr_pts_for_match_win": "1.0",
      "rr_pts_for_match_tie": "0.5",
      "created_by_api": false,
      "credit_capped": false,
      "category": null,
      "hide_seeds": false,
      "prediction_method": 0,
      "predictions_opened_at": null,
      "anonymous_voting": false,
      "max_predictions_per_user": 1,
      "signup_cap": null,
      "game_id": 485,
      "participants_count": 2,
      "group_stages_enabled": false,
      "allow_participant_match_reporting": false,
      "teams": false,
      "check_in_duration": null,
      "start_at": null,
      "started_checking_in_at": null,
      "tie_breaks": [
        "match wins vs tied",
        "game wins",
        "points scored"
      ],
      "locked_at": null,
      "event_id": null,
      "public_predictions_before_start_time": false,
      "ranked": false,
      "grand_finals_modifier": null,
      "predict_the_losers_bracket": false,
      "spam": null,
      "ham": null,
      "rr_iterations": 1,
      "tournament_registration_id": null,
      "donation_contest_enabled": null,
      "mandatory_donation": null,
      "non_elimination_tournament_data": {
        "participants_per_match": ""
      },
      "auto_assign_stations": null,
      "only_start_matches_with_stations": null,
      "registration_fee": "0.0",
      "registration_type": "free",
      "split_participants": false,
      "allowed_regions": [
        
      ],
      "show_participant_country": null,
      "program_id": null,
      "program_classification_ids_allowed": null,
      "team_size_range": null,
      "toxic": null,
      "description_source": "",
      "subdomain": null,
      "full_challonge_url": "https://challonge.com/20210526",
      "live_image_url": "https://challonge.com/20210526.svg",
      "sign_up_url": null,
      "review_before_finalizing": true,
      "accepting_predictions": false,
      "participants_locked": true,
      "game_name": "9-ball",
      "participants_swappable": false,
      "team_convertable": false,
      "group_stages_were_started": false
    }
  }
]

我正在尝试获取状态为“正在进行”的锦标赛并返回“url”,然后可以使用它来启动如上所述的完整 URL。

我创建了以下内容:

<?php
 $apikey = 'MyAPIKEY';
 $contents_api = fopen("https://api.challonge.com/v1/tournaments.json? 
state=all&api_key=$apikey", "r");
 $json = stream_get_contents($contents_api);
 $jsonData = json_decode($json, TRUE);
 print $jsonData[count($jsonData)-1]['tournament']['url'];
 ?>

现在我需要弄清楚如何获取这些信息并加载页面!!!!

再次,我知道这可能不是最好的提问方式,但是任何指引我正确方向的指导都会非常有帮助。

【问题讨论】:

  • 那么如果你想绕过文本框和按钮,那么用户将如何提供url的部分?
  • jQuery 1.11.0 发布于November 15th, 2013...您可能想使用更新的版本
  • 这是一个本地服务器,不暴露给网络。我想消除用户必须输入任何内容的步骤。 chromebook 在启动时启动网页。只想让页面打开当前正在进行的锦标赛,而不是用户必须输入任何内容。

标签: javascript html json api


【解决方案1】:

我能够得到我想要的东西:

 <?php
 $page = $_SERVER['PHP_SELF'];
 $sec = "10";
 $apikey = 'MYAPIKEY';
 $contents_api = fopen("https://api.challonge.com/v1/tournaments.json?state=all&api_key=$apikey", "r");
 $json = stream_get_contents($contents_api);
 $jsonData = json_decode($json, TRUE);
 //print $jsonData[count($jsonData)-1]['tournament']['url'];
 $inputvalue = $jsonData[count($jsonData)-1]['tournament']['url'];
 $url= "http://challonge.com/$inputvalue/module?selected-round-filter=2&show_final_results=0&multiplier=2&show_tournament_name=1&scale_to_fit=1"
 ?>
 <html>
     <head>
         <script>
     location.replace("<?php echo $url; ?>");
 </script>
     <meta http-equiv="refresh" content="<?php echo $sec ?>;URL='<?php echo $page?>'">
     </head>
 </html>

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多