获取Google字体列表

I want to get list of google web fonts in select box to select a font. I am trying following function, but it gives the error.

Code:

function get_google_fonts() {
    $url = "https://www.googleapis.com/webfonts/v1/webfonts?sort=alpha";
    $result = json_response( $url );
        $font_list = array();
        foreach ( $result->items as $font ) {
            $font_list[] .= $font->family;          
        }
        return $font_list;  
}

function json_response( $url )  {
    $raw = file_get_contents( $url, 0, null, null );
    $decoded = json_decode( $raw );
    return $decoded;
}

Error:

Warning: file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP.

If I change the https to http, I get this error:

file_get_contents(http://www.googleapis.com/webfonts/v1/webfonts?sort=alpha): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in

I guess this is because of PHP settings on my server, which I am unable to change. So, is there any alternative way to get the font list from Google? Thanks.

To allow https wraper you must have the php_openssl extension and enable allow_url_include

You can edit you php.ini to set these values :

extension=php_openssl.dll

allow_url_include = On

If these values doesn't exist add these lines .

If you can't edit your php.ini file, then you can set in on the PHP file :

ini_set('allow_url_fopen', 'on');
ini_set('allow_url_include', 'on');

You could also try using CURL instead of file_get_contents. CURL is much faster than file_get_contents

$url = "https://www.googleapis.com/webfonts/v1/webfonts?sort=alpha";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

echo $result;

Hope this helps. :)

I think Webfonts deny access to non-browser agents. However you can use the Webfont API. In fact you need an API key, so once you get it, you'll use URLs like

https://www.googleapis.com/webfonts/v1/webfonts?key=YOUR-API-KEY

It's all documented in the provided link.

Google requires SSL so make sure thats enabled on your server.

To fix this error, go to your php.ini file, find the line ;sslextension=php_openssl.dll and remove the semicolon.

http://php.net/manual/en/function.json-decode.php https://developers.google.com/webfonts/docs/developer_api

    var_dump(json_decode(file_get_contents('https://www.googleapis.com/webfonts/v1/webfonts')));