How to Get List of All Google Fonts using PHP

php
Published on November 20, 2019

Use-Cases of this code snippet

  • Know how to get a list of all available Google Fonts using its API.
  • Know how to call Google Fonts Developer API using PHP.

The list of all Google Fonts can be retrieved using the Google Web Fonts Developer API.

Sometimes the requirement may be such that we may need to get a list of all available Google Fonts, so that a user or an admin can select a font of his choice. This font can then be rendered in the website's frontend.

Getting all available Google Fonts is possible through Google Fonts Developer API. A HTTP GET request needs to be made to the below API endpoint :

https://www.googleapis.com/webfonts/v1/webfonts?key={{GOOGLE-API-KEY}}

Getting an API Key and Enabling Google Fonts API

  • The API Key can be obtained from Google Developers Console. A Google API project needs to be created, if not created before.

  • Next we need to activate the Web Fonts Developer API. This can be done by going to the Library tab, searching for this API and enabling it.

Making Google Fonts API Call in PHP

// google api key
$google_api_key = "--GOOGLE-API-KEY--";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/webfonts/v1/webfonts?key=" . $google_api_key);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
$fonts_list = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code != 200) 
    exit('Error : Failed to get Google Fonts list');

// echo out list of fonts
echo '<pre>';print_r($fonts_list);echo '</pre>';

As of writing this tutorial, the API returned 973 fonts. Below are the response fields from a single result.

Array (
    [kind] => webfonts#webfont
    [family] => Roboto
    [category] => sans-serif
    [variants] => Array
        (
            [0] => 100
            [1] => 100italic
            [2] => 300
            [3] => 300italic
            [4] => regular
            [5] => italic
            [6] => 500
            [7] => 500italic
            [8] => 700
            [9] => 700italic
            [10] => 900
            [11] => 900italic
        )

    [subsets] => Array
        (
            [0] => greek-ext
            [1] => latin-ext
            [2] => latin
            [3] => cyrillic-ext
            [4] => vietnamese
            [5] => cyrillic
            [6] => greek
        )

    [version] => v20
    [lastModified] => 2019-07-24
    [files] => Array
        (
            [100] => http://fonts.gstatic.com/s/roboto/v20/KFOkCnqEu92Fr1MmgWxPKTM1K9nz.ttf
            [100italic] => http://fonts.gstatic.com/s/roboto/v20/KFOiCnqEu92Fr1Mu51QrIzcXLsnzjYk.ttf
            [300] => http://fonts.gstatic.com/s/roboto/v20/KFOlCnqEu92Fr1MmSU5vAx05IsDqlA.ttf
            [300italic] => http://fonts.gstatic.com/s/roboto/v20/KFOjCnqEu92Fr1Mu51TjARc9AMX6lJBP.ttf
            [regular] => http://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Me5WZLCzYlKw.ttf
            [italic] => http://fonts.gstatic.com/s/roboto/v20/KFOkCnqEu92Fr1Mu52xPKTM1K9nz.ttf
            [500] => http://fonts.gstatic.com/s/roboto/v20/KFOlCnqEu92Fr1MmEU9vAx05IsDqlA.ttf
            [500italic] => http://fonts.gstatic.com/s/roboto/v20/KFOjCnqEu92Fr1Mu51S7ABc9AMX6lJBP.ttf
            [700] => http://fonts.gstatic.com/s/roboto/v20/KFOlCnqEu92Fr1MmWUlvAx05IsDqlA.ttf
            [700italic] => http://fonts.gstatic.com/s/roboto/v20/KFOjCnqEu92Fr1Mu51TzBhc9AMX6lJBP.ttf
            [900] => http://fonts.gstatic.com/s/roboto/v20/KFOlCnqEu92Fr1MmYUtvAx05IsDqlA.ttf
            [900italic] => http://fonts.gstatic.com/s/roboto/v20/KFOjCnqEu92Fr1Mu51TLBBc9AMX6lJBP.ttf
        )
)
  • family : Name of the Google Font.
  • subsets : List of scripts supported by the font.
  • variants : List of styles available for the font.
  • files : Font files for the each of the variants.

Changing the Sort Order of the Results

By default the API returns the results in a random order.

However the API endpoint also accepts a sort parameter that can be used to change the order of the results. The permitted values are :

  • alpha : Retrieves the list in alphabetical order.
  • date : Retrieves the list in descending chronological order meaning the recent first.
  • popularity : Retrieves the list in descending order of popularity.
  • style : Retrieves the list with the font having the most styles at the top.
  • trending : Retrieves the list with the fastest growing font at the top.
https://www.googleapis.com/webfonts/v1/webfonts?key={{GOOGLE-API-KEY}}&sort={{SORT-FIELD}}

Useful Resources

In this Tutorial