keyid = $keyid; $serviceuri = ""; } /** * @param $uri string */ function call ($param = null) { $reqUri = $this->serviceUri . "keyid=" . $this->keyid; if ($param != null) { $reqUri .= $param->toUriQueryString(); } // request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $reqUri); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // $body = curl_exec($ch); if ($this->debug) print_r($body); return $this->buildResult($body); } /** * Handle result XML. * SHOULDO BE OVERIDE. */ function buildResult (&$body) { return null; } } /** * Base class of parameters for gnavi API services. */ class GNaviAPIParams { function GNaviAPIParams () { } /** * build Query String for calling service. * SHOULDO BE OVERIDE. */ function toUriQueryString () { return ""; } } /** * Base class of API results. */ class GNaviAPIResult { function GNaviAPIResult () { } } /** * This class represents the error response from gnavi API. * @property $code error code of gnavi API. */ class GNaviError { var $code; var $message; var $desc; // var $E_MESSAGE = array ( "600" => "NoShop", "601" => "Invalid Access", "602" => "Invalid Shop Number", "603" => "InvalidType", "604" => "Internal Server Error" ); var $E_DESC = array ( "600" => "指定された店舗の情報が存在しない。", "601" => "不正なアクセス(認証エラー)。", "602" => "不正なぐるなび店舗IDパラメータが指定された。", "603" => "不正な取得種別が指定された。", "604" => "処理中にエラーが発生した。" ); /** * constructor * @param $code error code */ function GNaviError ($code) { $this->code = $code; $this->message = $this->E_MESSAGE[$code]; $this->desc = $this->E_DESC[$code]; } /** * @static */ function createGNaviErrorFromXMLResponse (&$doc) { $nodeset = XMLUtils::getNodeSet($doc, "/gnavi/error/code/text()"); $code = $nodeset[0]->content; return new GNaviError($code); } /** * @static */ function isError(&$doc) { $nodeset = XMLUtils::getNodeSet($doc, "/gnavi/error"); return ($nodeset != null); } } // ////////////////////////////////////////////////////////// // 2-3-1. GNaviRestSearch // ////////////////////////////////////////////////////////// /** * Restrant search. */ class GNaviRestSearchAPI extends GNaviAPI { /** * Constructor. * @param $keyid access key gotten from gnavi. */ function GNaviRestSearchAPI ($keyid) { $this->keyid = $keyid; $this->serviceUri = "http://api.gnavi.co.jp/ver1/RestSearchAPI/?"; } /** * Build result object from response body. * @private */ function buildResult (&$body) { $doc = domxml_open_mem($body); if (GNaviError::isError($doc)) { return GNaviError::createGNaviErrorFromXMLResponse($doc); } else { return new GNaviRestSearchResult($doc); } } } /** * Parameters for RestSearchAPI. */ class GNaviRestSearchAPIParams extends GNaviAPIParams { var $id; var $name; var $name_kana; var $tel; var $address; var $area; var $pref; var $category_l; var $category_s; var $equipment; var $location; var $range; var $sort; var $offset; var $hit_per_page; var $offset_page; /** * constructor. * Set default value if it have. */ function GNaviRestSearchAPIParams() { // set default values if the param have it. $this->coodinates_mode = 1; $this->sort = 1; $this->offset = 1; $this->hit_per_page = 10; $this->offset_page = 1; } /** * build query string for calling service. */ function toUriQueryString() { $result = ""; if ($this->id != null) { $result .= "&id=" . $this->id; } if ($this->name != null) { $result .= "&name=" . $this->name; } if ($this->name_kana != null) { $result .= "&name_kana=" . $this->name_kana; } if ($this->tel != null) { $result .= "&tel=" . $this->tel; } if ($this->address != null) { $result .= "&address=" . $this->address; } if ($this->area != null) { $result .= "&area=" . $this->area; } if ($this->pref != null) { $result .= "&pref=" . $this->pref; } if ($this->category_l != null) { $result .= "&category_l=" . $this->category_l; } if ($this->category_s != null) { $result .= "&category_s=" . $this->category_s; } if ($this->equipment != null) { $result .= "&equipment=" . $this->equipment; } if ($this->location != null) { $tokyo = Datum::getInstance("Tokyo"); $loc = Molodensky::convert($this->location, $tokyo); $result .= "&cordinates_mode=1"; // Japanese Geodetic Datums 2000 $result .= "&latitude=" . $loc->getLat(); $result .= "&longitude=" . $loc->getLon(); } if ($this->range != null) { $result .= "&range=" . $this->range; } if ($this->sort != null) { $result .= "&sort=" . $this->sort; } if ($this->offset != null) { $result .= "&offset=" . $this->offset; } if ($this->hit_per_page != null) { $result .= "&hit_per_page=" . $this->hit_per_page; } if ($this->offset_page != null) { $result .= "&offset_page=" . $this->offset_page; } return $result; } /** * set parameters to get next page. */ function nextPage() { ++ $this->offset_page; } } /** * This class represents RestSearch results. */ class GNaviRestSearchResult extends GNaviAPIResult { var $restList; var $total_hit_count; var $hit_per_page; var $page_offset; function GNaviRestSearchResult (&$doc) { $response = XMLUtils::getNodeSet($doc, "/response"); $values = XMLUtils::getElementTextArray($response[0]); $this->total_hit_count = $values["total_hit_count"]; $this->hit_per_page = $values["hit_per_page"]; $this->page_offset = $values["page_offset"]; // $rests = XMLUtils::getNodeSet($doc, "/response/rest"); $this->restList = array(); $tokyo = Datum::getInstance("Tokyo"); foreach ($rests as $rest) { $values = XMLUtils::getElementTextArray($rest); $location = $tokyo->createGeoPoint($values["latitude"], $values["longitude"]); $catCodeLs = XMLUtils::getNodeSet($rest, "./code/category_code_l"); $catL = array(); foreach ($catCodeLs as $catCodeL) { $code = $catCodeL->get_content(); $order = XMLUtils::getContent($catCodeL, "@order"); $name = XMLUtils::getContent($rest, "./code/category_name_l[@order=\"$order\"]"); array_push($catL, new CategoryLarge($code, $name)); } $catCodeSs = XMLUtils::getNodeSet($rest, "./code/category_code_s"); $catS = array(); foreach ($catCodeSs as $catCodeS) { $code = $catCodeS->get_content(); $order = XMLUtils::getContent($catCodeS, "@order"); $name = XMLUtils::getContent($rest, "./code/category_name_s[@order=\"$order\"]"); array_push($catS, new CategorySmall($code, $name)); } $catCodeSs = XMLUtils::getNodeSet($rest, "./code/category_code_s"); $restaurant = new Restaurant ( $values["id"], $values["update_date"], $values["name"], $values["name_kana"], $location, $values["category"], $values["url"], $values["url_mobile"], XMLUtils::getContent($rest, "./image_url/shop_image1"), XMLUtils::getContent($rest, "./image_url/shop_image2"), XMLUtils::getContent($rest, "./image_url/qrcode"), $values["address"], $values["tel"], $values["fax"], $values["opentime"], $values["holiday"], XMLUtils::getContent($rest, "./access/line"), XMLUtils::getContent($rest, "./access/station"), XMLUtils::getContent($rest, "./access/station_exit"), XMLUtils::getContent($rest, "./access/walk"), XMLUtils::getContent($rest, "./access/note"), XMLUtils::getContent($rest, "./pr/pr_short"), XMLUtils::getContent($rest, "./pr/pr_long"), XMLUtils::getContent($rest, "./code/areacode"), XMLUtils::getContent($rest, "./code/areaname"), XMLUtils::getContent($rest, "./code/prefcode"), XMLUtils::getContent($rest, "./code/prefname"), $catL, $catS, $values["budget"], $values["equipment"], XMLUtils::getContent($rest, "./flags/mobile_site"), XMLUtils::getContent($rest, "./flags/mobile_coupon"), XMLUtils::getContent($rest, "./flags/pc_coupon") ); array_push ($this->restList, $restaurant); } } /** * Returns Array of Restaurant. */ function getRestList () { return $restList; } } class Restaurant { var $id; var $update_date; var $name; var $name_kana; var $location; var $category; var $url; var $url_mobile; var $shop_image; var $qrcode; var $address; var $tel; var $fax; var $opentime; var $holiday; var $line; var $station; var $station_exit; var $walk; var $note; var $pr_short; var $pr_long; var $areacode; var $areaname; var $prefcode; var $prefname; var $category_l; var $category_s; var $budget; var $equipment; var $mobile_site_flag; var $mobile_coupon_flag; var $pc_coupon_flag; /** * Constructor; */ function Restaurant($id, $update_date, $name, $name_kana, $location, $category, $url, $url_mobile, $shop_image1, $shop_image2, $qrcode, $address, $tel, $fax, $opentime, $holiday, $line, $station, $station_exit, $walk, $note, $pr_short, $pr_long, $areacode, $areaname, $prefcode, $prefname, $category_l, $category_s, $budget, $equipment, $mobile_site_flag, $mobile_coupon_flag, $pc_coupon_flag) { $this->id = $id; $this->update_date = $update_date; $this->name = $name; $this->name_kana = $name_kana; $this->location = $location; $this->category = $category; $this->url = $url; $this->url_mobile = $url_mobile; $this->shop_image = array($shop_image1, $shop_image2); $this->qrcode = $qrcode; $this->address = $address; $this->tel = $tel; $this->fax = $fax; $this->opentime = $opentime; $this->holiday = $holiday; $this->line = $line; $this->station = $station; $this->station_exit = $station_exit; $this->walk = $walk; $this->note = $note; $this->pr_short = $pr_short; $this->pr_long = $pr_long; $this->areacode = $areacode; $this->areaname = $areaname; $this->prefcode = $prefcode; $this->prefname = $prefname; $this->category_l = $category_l; $this->category_s = $category_s; $this->budget = $budget; $this->equipment = $equipment; $this->mobile_site_flag = $mobile_site_flag; $this->mobile_coupon_flag = $mobile_coupon_flag; $this->pc_coupon_flag = $pc_coupon_flag; } /** shop id */ function getId() { return $this->id; } /** update date */ function getUpdateDate () { return $this->update_date; } /** ship name */ function getName() { return $this->name; } /** shop name in kana */ function getNameKana() { return $this->name_kana; } /** shop location as GeoPoint object */ function getLocation() { return $this->location; } /** shop category */ function getCategory() { return $this->category; } /** url */ function getUrl() { return $this->url; } /** url for mobile */ function getUrlMobile() { return $this->url_mobile; } /** Array of shop image url */ function getShopImage() { return $this->shop_image; } /** QR code url */ function getQrcode() { return $this->qrcode; } /** address */ function getAddress() { return $this->address; } /** tel number */ function getTel() { return $this->tel; } /** FAX number */ function getFax() { return $this->fax; } /** open time */ function getOpenTime() { return $this->opentime; } /** holiday */ function getHoliday() { return $this->holiday; } /** train line */ function getLine() { return $this->line; } /** station name */ function getStation() { return $this->station; } /** station exit */ function getStationExit() { return $this->station_exit; } /** time to walk */ function getWalk() { return $this->walk; } /** note */ function getNote() { return $this->note; } /** PR script (short version) */ function getPrShort() { return $this->pr_short; } /** PR script (long version) */ function getPrLong() { return $this->pr_long; } /** area code */ function getAreaCode() { return $this->areacode; } /** area name */ function getAreaName() { return $this->areaname; } /** pref code */ function getPrefCode() { return $this->prefcode; } /** pref name */ function getPrefName() { return $this->prefname; } /** Array of CategoryLarge*/ function getCategoryL() { return $this->category_l; } /** Array of CategorSmall*/ function getCategoryS() { return $this->category_s; } /** budget */ function getBudget() { return $this->budget; } /** equipment */ function getEquipment() { return $this->equipment; } /** has mobile site flag */ function getMobileSiteFlag() { return $this->mobile_site_flag; } /** has mobile coupon flag */ function getMobileCouponFlag() { return $this->mobile_coupon_flag; } /** has PC coupon flag */ function getPcCouponFlag() { return $this->pc_coupon_flag; } } // ////////////////////////////////////////////////////////// // 2-3-2. AreaSearchAPI // ////////////////////////////////////////////////////////// class GNaviAreaSearchAPI extends GNaviAPI { /** */ function GNaviAreaSearchAPI ($keyid) { $this->keyid = $keyid; $this->serviceUri = "http://api.gnavi.co.jp/ver1/AreaSearchAPI/?"; } /** * Build result object from response body. */ function buildResult (&$body) { $doc = domxml_open_mem($body); if (GNaviError::isError($doc)) { return GNaviError::createGNaviErrorFromXMLResponse($doc); } else { return new GNaviAreaSearchResult($doc); } } } class GNaviAreaSearchResult extends GNaviAPIResult { var $areaList; function GNaviAreaSearchResult (&$doc) { $areas = XMLUtils::getNodeSet($doc, "/response/area"); $this->areaList = array(); foreach ($areas as $area) { $values = XMLUtils::getElementTextArray($area); $code = $values["area_code"]; $name = $values["area_name"]; $this->areaList[$code] = new Area($code, $name); } } } class Area { var $code; var $name; function Area($code, $name) { $this->code = $code; $this->name = $name; } function getCode() { return $this->code; } function getName() { return $this->name; } } // ////////////////////////////////////////////////////////// // 2-3-3. PrefSearchAPI // ////////////////////////////////////////////////////////// class GNaviPrefSearchAPI extends GNaviAPI { /** */ function GNaviPrefSearchAPI ($keyid) { $this->keyid = $keyid; $this->serviceUri = "http://api.gnavi.co.jp/ver1/PrefSearchAPI/?"; } /** */ function buildResult (&$body) { $doc = domxml_open_mem($body); if (GNaviError::isError($doc)) { return GNaviError::createGNaviErrorFromXMLResponse($doc); } else { return new GNaviPrefSearchResult($doc); } } } class GNaviPrefSearchResult extends GNaviAPIResult { var $prefList; function GNaviPrefSearchResult (&$doc) { $prefs = XMLUtils::getNodeSet($doc, "/response/pref"); $this->prefList = array(); foreach ($prefs as $pref) { $values = XMLUtils::getElementTextArray($pref); $code = $values["pref_code"]; $name = $values["pref_name"]; $areaCode = $values["area_code"]; $this->prefList[$code] = new Pref($code, $name, $areaCode); } } } class Pref { var $code; var $name; var $areaCode; function Pref($code, $name, $areaCode) { $this->code = $code; $this->name = $name; $this->areaCode = $areaCode; } function getCode() { return $this->code; } function getName() { return $this->name; } function getAreaCode() { return $this->areaCode; } } // ////////////////////////////////////////////////////////// // 2-3-4. CategoryLargeSearchAPI // ////////////////////////////////////////////////////////// class GNaviCategoryLargeSearchAPI extends GNaviAPI { /** */ function GNaviCategoryLargeSearchAPI ($keyid) { $this->keyid = $keyid; $this->serviceUri = "http://api.gnavi.co.jp/ver1/CategoryLargeSearchAPI/?"; } /** */ function buildResult (&$body) { $doc = domxml_open_mem($body); if (GNaviError::isError($doc)) { return GNaviError::createGNaviErrorFromXMLResponse($doc); } else { return new GNaviCategoryLargeSearchResult($doc); } } } class GNaviCategoryLargeSearchResult extends GNaviAPIResult { var $categoryLargeList; function GNaviCategoryLargeSearchResult (&$doc) { $catLs = XMLUtils::getNodeSet($doc, "/response/category_l"); $this->categoryLargeList = array(); foreach ($catLs as $catL) { $values = XMLUtils::getElementTextArray($catL); $code = $values["category_l_code"]; $name = $values["category_l_name"]; $this->categoryLargeList[$code] = new CategoryLarge($code, $name); } } } class CategoryLarge { var $code; var $name; function CategoryLarge($code, $name) { $this->code = $code; $this->name = $name; } function getCode() { return $this->code; } function getName() { return $this->name; } } // ////////////////////////////////////////////////////////// // 2-3-5. CategorySmallSearchAPI // ////////////////////////////////////////////////////////// class GNaviCategorySmallSearchAPI extends GNaviAPI { /** */ function GNaviCategorySmallSearchAPI ($keyid) { $this->keyid = $keyid; $this->serviceUri = "http://api.gnavi.co.jp/ver1/CategorySmallSearchAPI/?"; } /** */ function buildResult (&$body) { $doc = domxml_open_mem($body); if (GNaviError::isError($doc)) { return GNaviError::createGNaviErrorFromXMLResponse($doc); } else { return new GNaviCategorySmallSearchResult($doc); } } } class GNaviCategorySmallSearchResult extends GNaviAPIResult { var $categorySmallList; function GNaviCategorySmallSearchResult (&$doc) { $catSs = XMLUtils::getNodeSet($doc, "/response/category_s"); $this->categorySmallList = array(); foreach ($catSs as $catS) { $values = XMLUtils::getElementTextArray($catS); $code = $values["category_s_code"]; $name = $values["category_s_name"]; $categoryLCode = $values["category_l_code"]; $this->categorySmallList[$code] = new CategorySmall($code, $name, $categoryLCode); } } } class CategorySmall { var $code; var $name; var $categoryLCode; function CategorySmall($code, $name, $categoryLCode = null) { $this->code = $code; $this->name = $name; $this->categoryLCode = $categoryLCode; } function getCode() { return $this->code; } function getName() { return $this->name; } function getCategoryLCode() { return $this->categoryLCode; } } /** */ class XMLUtils { function getNodeSet(&$node, $xpath) { $doc = $node->owner_document(); $xpathCtx = $doc->xpath_new_context(); $xpr = $xpathCtx->xpath_eval($xpath, $node); return $xpr->nodeset; } function getElementTextArray(&$nodeSet) { $result = array(); foreach ($nodeSet->child_nodes() as $node) { if ($node->type == 1) { $name = $node->tagname; $value = $node->get_content(); $result[$name] = $value; } } return $result; } function getContent(&$node, $xpath) { $nodeSet = XMLUtils::getNodeSet($node, $xpath); if (count($nodeSet) == 0) { return null; } return $nodeSet[0]->get_content(); } } ?>