Jennifer Hamilton
Jennifer Hamilton

Reputation: 97

Use API JSON to display a URL for an HTML Page

I am completely new to all of this so please forgive any issues with how I'm describing and name things. I have an HTML page where the user enters a portion of a URL string in order to launch a new window with the complete URL string. I'd rather just lookup the portion of the URL using an API to create a JSON file and find it automatically. Any ideas how I can accomplish this without user intervention?

HTML (contains text box and button to launch a new window, which I would like to bypass this altogether)

<!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>

My sample API call is as follows:

[
  {
    "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
    }
  }
]

Where I am trying to grab a tournament that the state is "underway" and return "url" that can then be used to launch a complete URL as described above.

I've created the following:

<?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'];
 ?>

Now I need to figure out how to grab this information and load the page!!!!

Again, I know this is probably not the best way to ask however, any guidance pointing me in the correct direction would be very helpful.

Upvotes: 0

Views: 428

Answers (1)

Jennifer Hamilton
Jennifer Hamilton

Reputation: 97

I was able to get what I wanted using:

 <?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>

Upvotes: 1

Related Questions