【PHP】ブログカードを作る

iframe などを使用できないサイト用に、ブログカードのHTMLソースを作るPHPをプログラムしてみた。


ブックマークレットの javascript などを使って、以下のPHPを呼び出す仕組み。


<?php

if (!isset($_GET["url"])) {
    echo "url is Nothing !";
    exit;
}

$html = file_get_contents($_GET["url"]);
preg_match_all( "<meta property=\"og:([^\"]+)\" content=\'([^\"]+)\'>", $html, $ogp );
for( $i = 0; $i < count($ogp[1]); $i++ ) {
    $result[$ogp[1][$i]] = $ogp[2][$i];
}

$title = $result["title"];
$url = $result["url"];
if($url == ""){
    $url = $_GET["url"];
}

$img = $result["image"];
$cite = parse_url($url, PHP_URL_HOST);

  $style_blogcard .='
<style>
.blogcard {
  line-height: 1;
  background-color: #ffffff;
  border: 1px solid #eeeeee;
  word-wrap: break-word;
  margin: 40px;
  box-shadow: 0 0 10px 6px rgba(0,0,0,.025);
  min-height: 130px;
}
.blogcard a {
  text-decoration: none;
  opacity: 1;
  transition: all 0.2s ease;
}
.blogcard a:hover {
  opacity: 0.6;
}
.blogcard_thumbnail {
  float: left;
  padding: 20px;
}
.blogcard_title {
  font-size: 1em;
  font-weight: bold;
  line-height: 1.4;
  padding: 17px 20px 10px;  
}
.blogcard_cite {
  font-size: 0.85em;
  line-height: 1.6;
  padding: 0 17px 15px 20px;
}
</style>
  ';

echo show_blogcard($url,$title,$img,$cite);

function show_blogcard($url,$title,$img,$cite) {

  global $style_blogcard;

  $img_width ="100";
  $img_height = "100";
  $no_image = 'https://XXX/thumbnail.png';

  if($img == ""){
    $img = $no_image;
  }

  $img_tag = '<img src="'. $img .'" alt="'. $title .'" width="'. $img_width .'" height="'. $img_height .'" />';

  $sc_blogcard .= '
  '.$style_blogcard.'
  <div class="blogcard">
  <a href="'. $url .'">
   <div class="blogcard_thumbnail">'. $img_tag .'</div>
   <div class="blogcard_content">
    <div class="blogcard_title">'. $title .'</div>
    <div class="blogcard_cite">'. $cite .'</div>
   </div>
   <div class="clear"></div>
  </a>
  </div>';  
  return $sc_blogcard;  
}