├── .gitignore ├── inc ├── footer.php ├── right.php ├── left.php └── header.php ├── structure.sql ├── config.php ├── functions.php ├── templates └── livescores.html ├── css └── style.css ├── config.example.ini ├── README.md ├── index.php └── classes └── LiveScoreApi.class.php /.gitignore: -------------------------------------------------------------------------------- 1 | nbproject/ 2 | config.ini 3 | -------------------------------------------------------------------------------- /inc/footer.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /inc/right.php: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 |
5 | 6 | -------------------------------------------------------------------------------- /inc/left.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 |
5 |
-------------------------------------------------------------------------------- /inc/header.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | -------------------------------------------------------------------------------- /structure.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `cache` ( 2 | `url` int(11) NOT NULL, 3 | `json` longtext NOT NULL, 4 | `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 5 | PRIMARY KEY (`url`), 6 | UNIQUE KEY `url` (`url`) 7 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 8 | -------------------------------------------------------------------------------- /config.php: -------------------------------------------------------------------------------- 1 | setTimezone(new DateTimeZone($timezone)); 7 | $time= $date->format('H:i'); 8 | return $time; 9 | } 10 | -------------------------------------------------------------------------------- /templates/livescores.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 | 22 5 |
6 |
7 | Real Madrid 8 |
9 |
10 | 2 - 2 11 |
12 |
13 | Barcelona 14 |
15 |
16 |
17 | -------------------------------------------------------------------------------- /css/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #18191B; 3 | } 4 | 5 | .match-line { 6 | background-color: #26282B; 7 | margin-bottom: 10px; 8 | padding: 5px; 9 | } 10 | 11 | .team-name { 12 | color: #fff; 13 | } 14 | 15 | .team-name.right { 16 | text-align: right; 17 | } 18 | 19 | .time-box, 20 | .score-box { 21 | background-color: #121212; 22 | color: #fff; 23 | font-weight: bold; 24 | text-align: center; 25 | } 26 | -------------------------------------------------------------------------------- /config.example.ini: -------------------------------------------------------------------------------- 1 | KEY=AAAAAAAAAAAAAAAA ; the API key that you get from the live-score-api.com website 2 | SECRET=BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB ; the API secret that you get from the live-score-api.com website 3 | DB_HOST = 'localhost' ; the webser where your database is running 4 | DB_NAME = 'ls_local' ; the name of the database that we are going to use 5 | DB_USER = 'root' ; the username with which we are going to access the database 6 | DB_PASS = 'root' ; the password with which we are going to authenticate 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Setup 2 | 3 | 1. Rename `config.example.ini` to `config.ini`. This will allow `config.php` to 4 | be able to read our configuration settings. 5 | 2. Use the MySQL table structure provided in `structure.sql` to create the MySQL 6 | table where the LiveScoreApi class is going to cache the response from the 7 | live-score-api.com endpoints 8 | 9 | # Making your first call to get the livescores 10 | 11 | ``` 12 | $LiveScoreApi = new LiveScoreApi(KEY, SECRET, DB_HOST, DB_USER, DB_PASS, DB_NAME); 13 | echo '
';
14 | var_dump($LiveScoreApi->getLivescores());
15 | ```
16 | 


--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
 1 | getLivescores();
11 | 
12 | include 'inc/header.php';
13 | include 'inc/left.php';
14 | 
15 | foreach ($scores as $_score) { ?>
16 | 
17 | 
18 |
19 |
20 | 21 |
22 |
23 | 24 |
25 |
26 | 27 |
28 |
29 | 30 |
31 |
32 |
33 | 34 | 39 | 40 | _key = $key; 46 | $this->_secret = $secret; 47 | $this->connection = mysqli_connect($host, $user, $pass); 48 | $this->connection->select_db($db); 49 | } 50 | 51 | /** 52 | * Builds the URL with which we can access the Live Score API data 53 | * 54 | * @param string $endpoint the API endpoint to be accessed 55 | * @param array $params the parameters to be provided to the endpoint 56 | * @return string the full URL to be called 57 | */ 58 | protected function _buildUrl($endpoint, $params) { 59 | $params['key'] = $this->_key; 60 | $params['secret'] = $this->_secret; 61 | return $this->_baseUrl . $endpoint . '?' . http_build_query($params); 62 | } 63 | 64 | /** 65 | * Gets the live scores 66 | * 67 | * @param array $params filter para meters 68 | * @return array with live scores data 69 | */ 70 | public function getLivescores($params = []) { 71 | $url = $this->_buildUrl('scores/live.json', $params); 72 | $data = $this->_makeRequest($url); 73 | return $data['match']; 74 | } 75 | 76 | /** 77 | * Makes the actual HTTP request to the Live Score API services 78 | * if possible it will get the data from the cache 79 | * 80 | * @param string $url the Live Score API endpoint to be called 81 | * @return array with data 82 | * @throws RuntimeException if there is something wrong with the request 83 | */ 84 | protected function _makeRequest($url) { 85 | $json = $this->_useCache($url); 86 | 87 | if ($json) { 88 | $data = json_decode($json, true); 89 | } else { 90 | $json = file_get_contents($url); 91 | $data = json_decode($json, true); 92 | 93 | if (!$data['success']) { 94 | throw new RuntimeException($data['error']); 95 | } 96 | 97 | $this->_saveCache($url, $json); 98 | } 99 | 100 | return $data['data']; 101 | } 102 | 103 | /** 104 | * Loads a URLs cached response if it is still applicable 105 | * 106 | * @param string $url the Live Score API endpoint which response was cached 107 | * @return boolean|string false if the cache has become invalid otherwise 108 | * the JSON response that was cached 109 | */ 110 | protected function _useCache($url) { 111 | $url = mysqli_escape_string($this->connection, crc32($url)); 112 | $query = "SELECT json FROM cache WHERE url = '$url' AND time > (NOW()-INTERVAL 60 SECOND)"; 113 | $result = mysqli_query($this->connection, $query); 114 | if (!$result) { 115 | return false; 116 | } 117 | 118 | if (!mysqli_num_rows($result)) { 119 | return false; 120 | } 121 | 122 | $row = mysqli_fetch_assoc($result); 123 | return $row['json']; 124 | } 125 | 126 | /** 127 | * Saves a response from the Live Score API endpoints to the 128 | * cache table so it can be reused and hourly quote can be 129 | * spared 130 | * 131 | * @param string $url the Live Score API URL that was called 132 | * @param strning $json the JSON that was returned by the endpoint 133 | */ 134 | protected function _saveCache($url, $json) { 135 | $url = mysqli_escape_string($this->connection, crc32($url)); 136 | $json = mysqli_escape_string($this->connection, $json); 137 | 138 | $query = "INSERT INTO cache (url, json, time) VALUES ('$url', '$json', NOW()) 139 | ON DUPLICATE KEY UPDATE json = '$json', `time` = NOW()"; 140 | mysqli_query($this->connection, $query); 141 | } 142 | } 143 | 144 | --------------------------------------------------------------------------------