レ点腫瘍学ノート

Top / pukiwikiカスタマイズ箇所 / 自作プラグイン / ogp.inc.php

opengraph.php改変

bed8b650df.png

opengraph.phpはOGP情報を取得してブログやWEBサイトの情報量を増やして豊かなサイトを作るのに非常に有用です。しかし、クッキーに未対応であるが故にopengraph.phpを使っても取得できないページがあり、困っていました。このページではその解決策を記載します。

GitHub - scottmac/opengraph: Helper class for accessing the OpenGraph Protocol
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」という情報のみが返ってきます)。

Estimating the impact of the COVID-19 pandemic on diagnosis and survival of five cancers in Chile from 2020 to 2030: a simulation-based analysis
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

fileopengraph.php
fileogp.inc.php

ver 1.7

ファイル名の _0 を抜いて使ってください。

fileopengraph_0.php
fileogp_0.inc.php

この記事に対するコメント

最新の10件を表示しています。 コメントページを参照

  • さらにOGP取得に伴う無限ループの問題を解決するために、ogp.inc.phpのver 1.7リリースと同時にopengraph.phpもさらに改変を加えました。具体的にはUser Agent周りを変更しています。変更箇所はこちらを参照してください。 -- m0370 2023-01-14 (土) 10:58:10

お名前:


添付ファイル: fileogp_0.inc.php 159件 [詳細] fileopengraph_0.php 154件 [詳細] fileogp.inc.php 274件 [詳細] fileopengraph.php 281件 [詳細]

更新日:2023-01-13 閲覧数:537 views.