2

フォントを選択するために、選択ボックスで Google Web フォントのリストを取得したいと考えています。次の関数を試していますが、エラーが発生します。

コード:

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;
}

エラー:

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

https を http に変更すると、次のエラーが発生します。

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

これは、変更できないサーバーの PHP 設定が原因だと思います。では、Google からフォント リストを取得する別の方法はありますか? ありがとう。

4

3 に答える 3

6

httpsラッパーを許可するには、php_openssl拡張機能が必要であり、有効にする必要がありますallow_url_include

php.inあなたはこれらの値を設定するためにあなたを編集することができます:

extension=php_openssl.dll

allow_url_include = On

これらの値が存在しない場合は、これらの行を追加してください。

php.iniファイルを編集できない場合は、PHPファイルに設定できます。

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

CURLの代わりに使用してみることもできますfile_get_contentsCURLよりもはるかに高速です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;

お役に立てれば。:)

于 2012-09-27T06:45:59.393 に答える
3

Web フォントは、ブラウザ以外のエージェントへのアクセスを拒否していると思います。ただし、Webfont APIは使用できます。実際には API キーが必要なので、取得したら次のような URL を使用します。

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

提供されたリンクにすべて文書化されています。

于 2012-09-27T06:54:24.213 に答える
1

GoogleではSSLが必要なので、サーバーでSSLが有効になっていることを確認してください。

このエラーを修正するには、php.iniファイルに移動し、; sslextension = php_openssl.dll行を見つけて、セミコロンを削除します。

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')));
于 2012-09-27T08:16:56.977 に答える