Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey, so i am learning how to work with APIs and PHP just for fun.
I have a small web app using HTML, PHP and MAMP.

I have Soccer data displayed in a table on a page called "liveScores.php" with a unique ID for each game. When I click this link it opens a new page ("gameInfo.php") where i would like to delve deeper into the API for that game. However it is not working. It only works when I hardcode the ID but of course thats a waste of time as the IDs for each game changes. The id is correctly passed through my url but its just i do not know how to pull the data from it.

*UPDATE

When i click the link of the id it directs me to the new page. But no data is displayed.
Its simply an empty table with only the column names displayed.
When I run it through postman its the same.
Hardcode the ID in and i get data in both.


1. My method

===============

   public function getMatchInfo() {

    $url = $this->buildUrl('scores/events.json?
            key=Ka88B6jZrxO8dDQt&secret=L233yXpNCWQDZyxJIGSkNbjeI8nWLdqw&id='
            .$id);
    return $this->makeRequest($url);
}


2. My HTML for the GameData Screen

=================

<?php foreach ($data['data']['event'] as $_event){ ?>
                        <tr>

                            <td style="text-align: center;"><?= $_event['id'] ?></td>
                            <td style="text-align: center;"><?= $_event['player'] ?></td>

                            </tr>

                        <?php } ?>
                        <tr>


3. The API itself

========================

{
    "success": true,
    "data": {
        "event": [
            {
                "id": "10509914",
                "match_id": "129180",
                "player": "DEENEY TROY",
                "time": "5",
                "event": "GOAL",
                "sort": "0",
                "home_away": "h"
            },
            {
                "id": "10509915",
                "match_id": "129180",
                "player": "MARIAPPA ADRIAN",
                "time": "13",
                "event": "YELLOW_CARD",
                "sort": "1",
                "home_away": "h"
            },


What I have tried:

If i hard code the id in it works fine for that particular row of data. So i presume my issue lies in the "GameInfo" screen as although the ID is there in the URL it is not able to pull the correct info.
Posted
Updated 9-Aug-20 21:41pm
v2
Comments
Garth J Lancaster 10-Aug-20 3:23am    
I don't think you've shown us the relevant parts from which we can help you - for example, what runs after you click the link 'to get to' gameInfo.php ... you'd be correct in assuming that somewhere along the line, the id of the game/link you've clicked on, must be passed into/obtained by gameInfo, so it can then get the further details from the JSON and display those ... but without the relevant guts, we're more blind than you ... Use Improve question to update your question.
Member 14910719 10-Aug-20 3:41am    
Hey, When i click the link of the id it directs me to the new page. But no data is displayed.
Its simply an empty table with only the column names displayed.
When I run it through postman its the same.
Hardcode the ID in and i get data in both.
Richard MacCutchan 10-Aug-20 4:50am    
What is the value of $id when you send the URL to the API?
Member 14910719 11-Aug-20 15:02pm    
I pass the ID that is shown in my question, 129180. I get no data when I sue my current code but when i hardcore the id I get all the data.
Richard MacCutchan 11-Aug-20 15:08pm    
Well the only way to find out what is happening is to use the debugger, and examine everything as you step through the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900