[Windows Mobile]使用 Yahoo!奇摩生活+ API 查詢商家資訊

  • 10650
  • 0
  • 2013-04-15

使用 Yahoo!奇摩生活+ API 查詢商家資訊與照片

 

1. 簡介

Yahoo!奇摩生活+ 提供吃喝玩樂商家資訊搜尋功能,而Yahoo!奇摩生活+ API 提供簡單介面,讓自行開發的程式存取生活+ 內的商家資訊。

目前 Yahoo!奇摩生活+ API 最新版本為 Beta V0.3,可利用關鍵字查詢相關的商店資料,其中包含每一個商家完整的評價分數,照片以及評價。

 

image image

 

2. 方法

2.1 取得應用程式帳號

image

填寫相關資訊

image

取得 AppID,以供後續調用 API 使用

image

 

2.2 生活+ API 使用方式

在生活+API 方法列表中,可以了解運作方式為給網址,於網址下方法與參數,則回傳查詢結果,格式為 XML。

image

在此,我挑了三個方法做介紹

(1) 驗證 AppID

 image

在 2.1 中所取得的 AppID,需透過此方法做驗證,透過

http://tw.lifestyle.yahooapis.com/v0.3/Auth.bootUp?appid=您申請的AppID

以下為驗證成功時,回傳的 XML 檔如下所示

image

 

(2) Biz.search 搜尋商家

image

搜尋商家資訊可以說是生活+最主要的功能,透過

http://tw.lifestyle.yahooapis.com/v0.3/Biz.search?appid=您申請的AppID&BizName=店家名稱&photo=1&address&page=1

在這裡我們試著搜尋Burgerking,回傳的 XML 檔如下所示,而XML檔有使用 CDATA 類型,因此之後程式處理時,可以以此作為判斷

image

*註 : 在這邊取得的 PhotoUrl 照片網址不能使用

 

(3) Biz.GetPhotos 取得商家的照片

image

在上個步驟中,取得了 BizID,可透過它來取得商家的照片,透過

http://tw.lifestyle.yahooapis.com/v0.3/Biz.getPhotos?appid=您申請的AppID&ID=商家ID&begin=1&limit=100

以下為剛剛取得的商家ID FS5597S38519 ,回傳的 XML 檔如下所示

image

 

 

2.3 程式碼

以下程式碼使用了 Yahoo! 生活+ API 兩個功能,Biz.search:搜尋商家 與 Biz.getPhotos:取得商家的照片,取得 XML 檔後做資料處理與顯示。


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.IO;
using System.Xml;

namespace SmartDeviceYahooAPI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string AppID = "PLtuj_nV34GetDQ2lZkv_n1.ShfGDzbpPlz8IxcYUm_fZqQzkWGO0bI2o22GahR.4Q--"; // 修改成您的 Yahoo! App ID

        // Search Button
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string url = @"http://tw.lifestyle.yahooapis.com/v0.3/Biz.search?appid=" + this.AppID + @"&BizName=" + this.txtQuery.Text + @"&address=&page=1";
            string strBizId = string.Empty;

            try
            {
                // 建立 XML 讀取器
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true; // 不處理註解
                settings.IgnoreWhitespace = true; // 跳過空白
                settings.ValidationType = ValidationType.None; // 不驗證任何資料
                XmlReader reader = XmlTextReader.Create(url, settings);
                string sName = "";
                while (reader.Read())
                {

                    switch (reader.NodeType)// 取得目前節點型別
                    {
                        case XmlNodeType.Element:
                            sName = reader.Name;
                            if (sName == "Biz")  // 取得 BizID
                            {
                                string Name = reader["id"];
                                strBizId = Name;
                            }
                            break;
                        case XmlNodeType.CDATA:  // 當節點型別為 CDATA
                            switch (sName)
                            {
                                case "Name":
                                    txtName.Text = reader.Value.Replace("<em>", "").Replace("</em>", "");
                                    break;
                                case "Address":
                                    txtAddress.Text = reader.Value.Replace("<em>", "").Replace("</em>", "");
                                    break;
                                case "Tel":
                                    txtTel.Text = reader.Value.Replace("<em>", "").Replace("</em>", "");
                                    break;
                                case "Comment":
                                    txtComment.Text = reader.Value.Replace("<em>", "").Replace("</em>", "");
                                    break;
                                case "LatLong":
                                    txtLatLong.Text = reader.Value;
                                    GetPhoto(strBizId);  // 取得商家照片,傳入BizID
                                    return;
                                    break;
                            }
                            break;
                    }
                }
            }
            catch (System.Xml.XmlException xe)
            {
                MessageBox.Show(xe.Message);
            }
        }

        /// <summary>
        /// 取得商家圖片
        /// </summary>
        /// <param name="strBizId">BizID商家ID</param>
        private void GetPhoto(string strBizId)
        {
            this.lstPhoto.Items.Clear();
            if (strBizId == string.Empty)
            {
                return;
            }
            string url = @"http://tw.lifestyle.yahooapis.com/v0.3/Biz.getPhotos?appid=" + this.AppID + @"&ID=" + strBizId + @"&begin=1&limit=100";

            try
            {
                // 建立 XML 讀取器
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true; // 不處理註解
                settings.IgnoreWhitespace = true; // 跳過空白
                settings.ValidationType = ValidationType.None; // 不驗證任何資料
                XmlReader reader = XmlTextReader.Create(url, settings);
                string sName = "";
                while (reader.Read())
                {

                    switch (reader.NodeType)  // 取得目前節點型別
                    {
                        case XmlNodeType.Element:
                            sName = reader.Name;
                            break;
                        case XmlNodeType.CDATA:  // 當節點型別為 CDATA
                            switch (sName)
                            {
                                case "Url":
                                    this.lstPhoto.Items.Add(reader.Value);
                                    break;
                                //在此先不抓大中小圖
                                //case "SUrl":
                                //    this.listBox1.Items.Add(reader.Value);
                                //    break;
                                //case "MUrl":
                                //    this.listBox1.Items.Add(reader.Value);                                 
                                //    break;

                                //case "LUrl":
                                //    this.listBox1.Items.Add(reader.Value);
                                //    break;

                            }
                            break;
                    }
                }

                // 假如有ListBox中有照片網址的話,則選擇第一筆資料(index = 0)作為預設值
                if (this.lstPhoto.Items.Count > 0)
                {
                    this.lstPhoto.SelectedIndex = 0;
                }
            }
            catch (System.Xml.XmlException xe)
            {
                MessageBox.Show(xe.Message);
            }
        }

        private void lstPhoto_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lstPhoto.SelectedIndex > -1) // 當使用者選擇ListBox中的照片網址時
            {
                // 讀取網路圖片到 PictureBox
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(lstPhoto.SelectedItem.ToString());
                HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
                if (rsp.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    this.PicBoxPhoto.Image = new Bitmap(rsp.GetResponseStream());
                }
                rsp.Close();
            }
        }
    }
}

 

 

3. 結果

以下為搜尋 Starbucks 與 Burgerking 的結果,於 Windows Mobile 6.5 模擬(英文)

 

以下為搜尋 花園夜市 的結果,於 Windows Mobile 6 模擬 (中文)

image

 

4. 檔案下載

SmartDeviceYahooAPI.rar