Skip to content

Instantly share code, notes, and snippets.

@fanannan
Created July 11, 2014 03:09
Show Gist options
  • Save fanannan/2498e8af54defb923720 to your computer and use it in GitHub Desktop.
Save fanannan/2498e8af54defb923720 to your computer and use it in GitHub Desktop.
Retrieve info on a book from the National Diet Liberary
<?php
function check_tag($tag){
if (array_key_exists($tag, $_GET) && $_GET[$tag] <> ""){
$value = $_GET[$tag];
} else {
$value = "";
}
return $value;
}
class fetch_xml_as_array{
public static function fetch($api_url, $param, $type = 'GET'){
$data = http_build_query($param, '', '&');
$header = array('Content-Type: application/x-www-form-urlencoded', 'Content-Length: '.strlen($data));
$context = array('http' => array('method' => $type, 'header' => implode("\r\n", $header), 'content' => $data));
$xml = file_get_contents($api_url, false, stream_context_create($context));
if (false === $xml) return false;
$xml2 = preg_replace("/<(\/*)(\w+):(\w+)/",'<${1}${2}_${3}',$xml);
$name_space_replaced_xml = preg_replace("/<dc_identifier xsi:type=\"dcndl:ISBN\">(\d+)<\/dc_identifier>/",'<isbn>${1}</isbn>',$xml2);
$parse_xml = simplexml_load_string($name_space_replaced_xml,'SimpleXMLElement', LIBXML_NOCDATA);
if (false === $parse_xml) return false;
return self::conv_to_array($parse_xml);
}
public static function conv_to_array($xmlobj){
$arr = array();
if (is_object($xmlobj)){
$xmlobj = get_object_vars($xmlobj);
} else {
$xmlobj = $xmlobj;
}
foreach ($xmlobj as $key => $val) {
if (is_object($xmlobj[$key])) {
$arr[$key] = self::conv_to_array($val);
} else if (is_array($val)) {
foreach($val as $k => $v) {
if (is_object($v) || is_array($v)) {
$arr[$key][$k] = self::conv_to_array($v);
} else {
$arr[$key][$k] = $v;
}
}
} else {
$arr[$key] = $val;
}
}
return $arr;
}
}
function clear_author($s){
$s = preg_replace('/\s+共著,*\s*$/','',$s);
$s = preg_replace('/\s+共編,*\s*$/','',$s);
$s = preg_replace('/\s+[共著],*\s*$/','',$s);
$s = preg_replace('/\s+[共編],*\s*$/','',$s);
$s = preg_replace('/\s+[著],*\s*$/','',$s);
$s = preg_replace('/\s+[編],*\s*$/','',$s);
$s = preg_replace('/\s+[訳],*\s*$/','',$s);
$s = preg_replace('/\s+[共著],*\s*$/','',$s);
$s = preg_replace('/\s+[共編],*\s*$/','',$s);
$s = preg_replace('/\s+\[著\],*\s*$/','',$s);
$s = preg_replace('/\s+\[編\],*\s*$/','',$s);
$s = preg_replace('/\s+\[訳\],*\s*$/','',$s);
$s = preg_replace('/\s+著,*\s*$/','',$s);
$s = preg_replace('/\s+編,*\s*$/','',$s);
$s = preg_replace('/\s+訳,*\s*$/','',$s);
$s = preg_replace('/\s+他,*\s*$/','',$s);
$s = preg_replace('/\s+他著,*\s*$/','',$s);
$s = preg_replace('/\s+他編,*\s*$/','',$s);
$s = preg_replace('/\s+他訳,*\s*$/','',$s);
$s = preg_replace('/\s*,.*$/','',$s);
return $s;
}
function conv_date($s){
return date("Y-m-d", strtotime($s));
}
function output_attribute($key, $item, $callback = ""){
$r = "";
if (array_key_exists($key, $item)){
$r = $item{$key};
if($callback <> ""){
$r = call_user_func($callback, $r);
}
}
echo $r."\t";
}
function get_attributes($items){
foreach($items as $key => $item){
output_attribute("author", $item, 'clear_author'); //dc_creatorだと著者が複数の場合配列で返る
output_attribute("title", $item);
output_attribute("link", $item);
output_attribute("isbn", $item);
output_attribute("pubDate", $item, 'conv_date');
echo "\n";
}
}
////
//$_GET['isbn' ]='9784000069731';
date_default_timezone_set('Asia/Tokyo');
$tags = array('isbn', 'title', 'creator');
foreach($tags as $tag){
$value = check_tag($tag);
if ($value <> ""){
break;
}
}
if ($value <> ""){
$parsed = fetch_xml_as_array::fetch('http://iss.ndl.go.jp/api/opensearch?', array($tag => $value), 'GET');
$num_items = $parsed{"channel"}{"openSearch_totalResults"};
if($num_items == 0){
} else if($num_items == 1){
get_attributes(array($parsed{"channel"}{"item"}));
} else {
get_attributes($parsed{"channel"}{"item"});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment