2017 © Pedro Peláez
 

project xmlsoccer

An API client for the XML Soccer service

image

petercoles/xmlsoccer

An API client for the XML Soccer service

  • Sunday, March 5, 2017
  • by petercoles
  • Repository
  • 1 Watchers
  • 2 Stars
  • 32 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

XML Soccer API Client

SensioLabsInsight Scrutinizer Code Quality Build Status License, (*1)

The purpose of this package is to provide easy access to the XML Soccer API for retrieving historic and live data about football (soccer) matches., (*2)

Requirements

PHP 7.0+, Composer, SimpleXML, (*3)

Installation

The package is designed to be integrated into projects using Composer as their dependancy manager. To add to your project, navigate the root of the project and execute composer require petercoles/xml-soccer., (*4)

Basic Usage

To make an API request, you will need an API key. XML Soccer generously provides free demo API keys to allow you to test out the service. You can get one here., (*5)

Once you have an API key, instantiate an instance of the Api client and pass the key in as the first parameter. For requests to the demo system (e.g. if you don't yet have a paid-for subscription and are using a free API key or are testing and want more generous API throttling limits), set the second paramter to true. To access the full service, just omit the 2nd parameter. For example:, (*6)

``` php use PeterColes\XmlSoccer\ApiClient;, (*7)

class MyRequest() { ..., (*8)

public function getLiveData()
{
    $client = new ApiClient('YOUR_API_KEY_HERE', true));  // for the demo service

    $response = $client->getLiveScore()->get();
}

...

}, (*9)

This will return the raw data exactly as it's received from the XMLSoccer service, in pure XML.

A full list of the methods available can be found in the [XML Soccer service description](http://www.xmlsoccer.com/FootballData.asmx). More [general documentation for the service](https://xmlsoccer.zendesk.com/hc/en-us) is also available.

Many methods require parameters. These are passed as an associative array. For example:

``` php
$client->getNextMatchOddsByLeague([ 'league' => 3 ]);
$client->GetFixturesByLeagueAndSeason([ 'league' => 3, 'seasonDateString' => '0506' ]);

Transforming the Response

If you like parsing XML data manually then the ->get() method may be all you need ... feel free to skip the rest of this section., (*10)

Alternatively, this package makes available three more friendly ways to receive the response., (*11)

SimpleXML Object

Using the xml() method in place of get() will cause the response to be converted into a SimpleXMLElement object. These can be iterated through as shown below:, (*12)

``` php try { $xml = $client->getLiveScore()->xml(); } catch(\Exception $e) { exit('XML Soccer Exception: '.$e->getMessage()); }, (*13)

foreach ($xml->Match as $match) { echo "$match->HomeTeam v $match->AwayTeam : $match->HomeGoals-$match->AwayGoals"; }, (*14)


### PHP Object If you're not a big fan of XML, no problem. Using the object() method will transform the response into a PHP object. For example,```$object = $client->getLiveScore()->object();```. Most response objects are simple lists of attributes. So the getAllLeagues will return an object containing a League attribute, which in turn will contain an array of objects each describing one league (i.e. competition as it includes cups and hybrids such as The EUFA Champions League): ``` php object(stdClass) { ["League"]=> array(n) { [0]=> object(stdClass) { ["Id"] => "1", ["Name"] => "English Premier League", {"Country"] => "England", ... ["IsCup"] => "false" }, ... [n]=> object { ["Id"] => "57", ["Name"] => "Ligaat AL", {"Country"] => "Isreal", ... ["IsCup"] => "false" } } ["AccountInformation"]=> string "Blah." }

But match data, e.g. retrieved from Live Score or Historic Fixtures, have more complex structures with information about goals, cards, substititions and players, all collapsed into strings in the default response, that have to be unpacked if you're to make sense of them., (*15)

This package takes care of this too so when converted to an object this XML goal string, (*16)

``` xml 50': Riccardo Orsolini;38': Andrea Favilli;4':Own A N Other;, (*17)


would be returned as: ``` bash ... ["HomeGoalDetails"]=> array(3) { [0]=> object(stdClass) { ["Minute"] => 50, ["Player"] => "Riccardo Orsolini", ["Own"] => false }, [1]=> object(stdClass) { ["Minute"] => 38, ["Player"] => "Andrea Favilli", ["Own"] => false }, [2]=> object(stdClass) { ["Minute"] => 4, ["Player"] => "A N Othe", ["Own"] => true } ...

and the data within it accessed such as this, (*18)

php foreach ($object->Match[23]->HomeGoalDetails as $goal) { echo $goal->Minute; echo $goal->Player; echo $goal->Own; }, (*19)

... and similarly for cards, substitutions and player lists., (*20)

Note that for match data only, each value is cast to an appropriate type, i.e. integers for numeric data and boolean where appropriate, rather than everything defaulting to strings., (*21)

JSON

If you'd prefer to receive the object in JSON notation, i.e. as a JSON-encoded string, which can be useful for caching or passing to a browser-side application, then use the json() method instead, e.g. $json = $client->getLiveScore()->json();., (*22)

Upgrading

In version 1, data was returned by the call to an XMLSoccer-like method such as getLiveScore(). In version 2 however, it's returned by get(), xml(), object() or json() (see above for descriptions) according to your taste., (*23)

If you were using object() or json() with version 1, simply move this call to the end of the request, e.g.$json = $client->json()->getLiveScore(); becomes $json = $client->getLiveScore()->json();., (*24)

If you weren't using any conversions, then just append xml() to return a simpleXML object, as was the default in version 1, e.g. $xml = $client->getLiveScore(); becomes $xml = $client->getLiveScore()->xml();., (*25)

The Test Suite

Four sets of tests are provided, general unit tests, which simply check that the client operates as it should, integration tests, which connect (mostly) to the demo XML Soccer service and confirm that requests are transmitted and responses are as expected, and special suites for PHP object and JSON conversion., (*26)

Before running either set of tests, copy the phpunit.xml.dist to phpunit.xml. If you want to run the integration tests you will also need to edit this file to insert your XML Soccer API Key towards the end, where indicated. This new file will be excluded from any git commits that you make so your API Key will remain secret even if you make public contributions to this package., (*27)

To run the unit tests alone, simply execute vendor/bin/phpunit tests/UnitTest. Similarly, to run the integration tests only, execute vendor/bin/phpunit tests/IntegrationTest, or to run everything, just vendor/bin/phpunit., (*28)

if you only have a demo service key, then one of the integration tests may fail as it can only be run against the live service. If this happens, add the --exclude-group live flag to suppress that unimportant test., (*29)

Both live and demo APIs are throttled to avoid excessive load on the service's servers. Most calls to the Demo API require a 5 second gap between requests (for the live system the gaps are larger and depend on the specific request being sent). The integration test suite enforces this delay before running each affected test so if you run it, expect some 5 second pauses in execution., (*30)

The Versions

05/03 2017

dev-master

9999999-dev

An API client for the XML Soccer service

  Sources   Download

MIT

The Requires

 

The Development Requires

api xml soccer football data

28/02 2017

2.0.0

2.0.0.0

An API client for the XML Soccer service

  Sources   Download

MIT

The Requires

 

The Development Requires

api xml soccer football data

28/02 2017

1.2.0

1.2.0.0

An API client for the XML Soccer service

  Sources   Download

MIT

The Requires

 

The Development Requires

api xml soccer football data

25/02 2017

dev-develop

dev-develop

An API client for the XML Soccer service

  Sources   Download

MIT

The Requires

 

The Development Requires

api xml soccer football data

25/02 2017

1.1.0

1.1.0.0

An API client for the XML Soccer service

  Sources   Download

MIT

The Requires

 

The Development Requires

api xml soccer football data

03/02 2017

1.0.0

1.0.0.0

An API client for the XML Soccer service

  Sources   Download

MIT

The Requires

 

The Development Requires

api xml soccer football data