dongshang3309 2012-09-27 06:42
浏览 109
已采纳

获取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.

  • 写回答

3条回答 默认 最新

  • donglou1866 2012-09-27 06:45
    关注

    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. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条