Loading...

To get your Lifetime Free API key signup here

This section explains how to use Cricket Data API with different languages. We have made sure to include most languages - however if you'd like to contribute please do reach out to us! Also a generic 'how the Cricket Data API works' explanation can be found here.

PHP Coding

Concept Sample

How to execute Cricket Data API in PHP

The basic logic for JSON API like what we offer in Cricket Data remains the same - you need to FETCH the Cricket Data API and then parse it as JSON using JSON_DECODE. Since List APIs are paginated, you can retrieve multiple pages as we've shown in this basic sample.

Code Sample
<?php// We will fetch ALL pages of the Countries List// For a real live scenario, try to optimize your consumption$countriesList = array();$offset = 0;$maxOffset = 1;while($offset<$maxOffset) {    $json_result = file_get_contents('https://api.cricapi.com/v1/countries?apikey=[your api key]&offset='.$offset);    $j = json_decode($json_result);    if($j->status != "success") {        die("FAILED TO GET A SUCCESS RESULT");    }    $maxOffset = $j->info->totalRows;        $offset += count($j->data);    $countriesList = array_merge($countriesList,$j->data);}// Now the Countries List has ALL countries, you can use it for Display or to update your own Databaseecho "Final countries List contains ".count($countriesList)." elements";?>

C#.NET Coding

Concept Sample

How to execute Cricket Data API in C#

C# you can use WebClient or RestSharp to get similar results. You will also require a JSON library either NewtonSoft or you can use the JavascriptSerializer that comes inbuilt with dotNet Framework. The rest of it is easy, FETCH the Cricket Data API and then parse it as JSON. List APIs are paginated, you can retrieve multiple pages as we've shown in this basic sample.

Code Sample
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();System.Net.WebClient wc = new System.Net.WebClient();int ofset = 0;int maxofset = 1;List<Object> countryArray = new List<Object>();while (ofset < maxofset){    dynamic j = json.DeserializeObject(wc.DownloadString("https://api.cricapi.com/v1/countries?apikey=[your api key]&offset=" + ofset));    if (j["status"] != "success")    {        Response.Write("FAILED TO GET A SUCCESS RESULT");        break;    }    maxofset = j["info"]["totalRows"];    ofset += j["data"].Length;    countryArray.AddRange(j["data"]);}// countryArray now contains a full list of countries// Dump the entire result as a JSON (just to show that you can convert back and forth to JSON)Response.Write(json.Serialize(countryArray));

VB.NET Coding

Concept Sample

How to execute Cricket Data API in VB.NET

In VB.NET you can use the dependable System.Net.WebClient class to fetch the Cricket API. We will use the JavascriptSerializer that comes inbuilt with dotNet Framework. Then we'll FETCH the API and then parse the resultant String as JSON Object. List APIs are paginated, you can retrieve multiple pages as we've shown in this basic sample.

Code Sample
Dim json As New System.Web.Script.Serialization.JavaScriptSerializer()Dim wc As New System.Net.WebClient()Dim ofset As Integer = 0Dim maxofset As Integer = 1Dim countryArray As New List(Of Object)While ofset < maxofset    Dim j As Object = json.DeserializeObject(wc.DownloadString("https://api.cricapi.com/v1/countries?apikey=[your api key]&offset=" & ofset))    If j("status") <> "success" Then    	Response.Write("FAILED TO GET A SUCCESS RESULT")    	Exit While    End If    maxofset = j("info")("totalRows")    ofset += j("data").length    countryArray.AddRange(j("data"))End While' countryArray now contains a full list of countries' Dump the entire result as a JSON (just to show that you can convert back and forth to JSON)Response.Write(json.Serialize(countryArray))

Node.JS Coding

Concept Sample

How to execute Cricket Data API in Node.JS

Node.JS is basically javascript, so it's best to use the fetch method. For this install node-fetch and the following code will work well. After fetching the API, JSON.parse should be enough to convert the string into a usable format. List APIs are paginated, you can retrieve multiple pages as we've shown in this basic sample.

Code Sample
async function fetchFromOffset(offset) {    return await fetch("https://api.cricapi.com/v1/countries?apikey=[your api key]&offset=" + offset)        .then(data => data.json())        .then(data => {            if (data.status != "success") { alert("Failed"); return; }            let datarray = data.data;            if (!datarray)                return [];            else if (offset >= data.info.totalRows)                return datarray;            else                return fetchFromOffset(offset + 25)                    .then(function (data) {                        return datarray.concat(data);                    });        })        .catch(e => console.log);}fetchFromOffset(0)    .then(function (data) {        console.log("Complete data got!", data);    })    .catch(e => console.log);

JQuery Coding

Concept Sample

How to execute Cricket Data API in JQuery

JQuery has very convenient $.post method that can be used to fetch the Cricket API, JSON.parse should be enough to convert the string into a usable format. List APIs are paginated, you can retrieve multiple pages as we've shown in this basic sample.

Note: You should obfuscate your code to hide the API key. Using this in the frontend is not recommended, that can expose your API key to security risks.

Code Sample
function fetchFromOffset(offset, cb) {    $.post('https://api.cricapi.com/v1/countries?apikey=[your api key]&offset=' + offset, function(data) {        if(data.status!="success") { alert("Failed"); return; }        let datarray = data.data;        if(!datarray)             cb([]);        else if(offset >= data.info.totalRows)             cb(datarray);        else             fetchFromOffset(offset + 25, function(data) {                cb(datarray.concat(data));            });    });}fetchFromOffset(0, function(data) {    console.log("Complete data got!",data);});