opengraph.php改変
opengraph.phpはOGP情報を取得してブログやWEBサイトの情報量を増やして豊かなサイトを作るのに非常に有用です。しかし、クッキーに未対応であるが故にopengraph.phpを使っても取得できないページがあり、困っていました。このページではその解決策を記載します。
Helper class for accessing the OpenGraph Protocol. Contribute to scottmac/opengraph development by creating an account on GitHub.
https://github.com/scottmac/opengraph/
opengraph.phpがクッキーを取得できるようにする
curl_setopt($curl, CURLOPT_FAILONERROR, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 15); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
デフォルトではcURLの設定に関するopengraph.phpの55〜61行目は上記のようになっていました。しかしこれではクッキーを扱えないため、サイトによっては取得ができないことがありました。具体的にはthe Lancetという英語医学雑誌のサイトの情報が取得できなくて困っていました(クッキーが使えない状態でアクセスしようとすると「DEFINE_ME」という情報のみが返ってきます)。
In addition to a large projected surge in diagnosed cancer cases, we found that delays
in diagnosis will result in worse cancer stage at presentation, leading to worse survival
outcomes. These findings can help to inform surge capacity planning and highlight
the importance of ensuring appropriate health system capacity levels to detect and
care for the increased cancer cases in the coming years, while maintaining the timeliness
and quality of cancer care. Potential delays in treatment and adverse impacts on quality
of care, which were not considered in this model, are likely to contribute to even
more excess deaths from cancer than projected.
https://www.thelancet.com/journals/lanonc/article/PIIS1470-2045(21)00426-5/fulltext
pukiwiki プラグイン はてなブログの「ブログカード」は魅力的でPukiwikiにも導入したいと思い、自分で見よう見まねでプラグインを作成して個人用のPukiwikiで利用していましたが、せっかくですのでこの外部のOGPを取得してブログカードとして表示するPukiwikiプラグインを公開することにしました。外部のOGPを取得
そこで、このcURL設定項目の下に下記の2行を追記します。これは、そのセッションだけで有効なクッキーを利用可能にするための設定です。PHPエラーが出ないよう最後のセミコロンも忘れずに。
curl_setopt($curl, CURLOPT_COOKIESESSION, true); curl_setopt($curl, CURLOPT_COOKIEFILE, './tmp/cookie.txt');
これで、クッキーを必要とするサイトでもopengraph.phpを使ってOGP情報を取得することができるようになりました。
ver1.5
ver 1.7
ファイル名の _0 を抜いて使ってください。
この記事に対するコメント
最新の10件を表示しています。 コメントページを参照
添付ファイル: ogp_0.inc.php 429件 [詳細] opengraph_0.php 438件 [詳細] ogp.inc.php 546件 [詳細] opengraph.php 562件 [詳細]
更新日:2023-01-13 閲覧数:825 views.