Getting started with JSON and the Yelp API
I'm trying to use the Yelp API to get local restaurants depending on the user's latitude and longitude. The trouble is that I'm quite sure how to do this. Looking at the page that I need to retrieve the data from, it says that the type is text/plain. Can I use Ajax to do this or will I not be able to since it is a different domain?
EDIT: The page I am trying to retrieve the contents from is: http://api.yelp.com/business_review_search?term=yelp&tl_lat=37.9&tl_long=-122.5&br_lat=37.788022&br_long=-122.399797&limit=3&ywsid=w_HyGEVjKFHYZdJC9DQBHg
I could do this in PHP no problem using file_get_contents()开发者_Go百科 but the site is built using asp. Is there a similar function in asp? I could maybe use an intermediate page as a sort of proxy for getting and outputting the data.
You can do the whole thing in Classic ASP without having to use any other languages or pages. You will need one thing though - there is a library out there called JSON2.ASP you will need to parse the results. You can find out more about it here:
http://zend.lojcomm.com.br/entries/classic-asp-json-revisited/
https://github.com/nagaozen/asp-xtreme-evolution/blob/master/lib/axe/classes/Parsers/json2.asp
Once you have this you can get Yelp results and parse them easily in Classic ASP. I am doing this with the API 1.0 as it is a bit easier but you can use this as a starting point to do it in the API 2.0 if you want sure.
Below is the code which will return results for a restaurant search. You of course have to fill in your API code where YWSID is in the URL below. I pipe the results into an array and then build a table off of the array to display on the page, but once you have the data you can do with it whatever you want.
<!--#include file="JSON2.asp"-->
<%
set xmlHTTP = server.createobject("MSXML2.ServerXMLHTTP.6.0")
xmlHTTP.open "GET", "http://api.yelp.com/business_review_search?location=Clare%20Ireland&ywsid=XXXXXXXXXX&category=restaurants", false
xmlHTTP.send()
RawFeed = xmlHTTP.ResponseText
Set RawResults = JSON.parse(join(array(RawFeed)))
For Each YelpFeed In RawResults.Get("businesses")
If YelpFeed.Get("is_closed") = "False" Then
Response.write YelpFeed.Get("name") & "<br>" & vbNewLine
Response.write YelpFeed.Get("rating_img_url") & "<br>" & vbNewLine
Response.write YelpFeed.Get("address1") & "<br>" & vbNewLine
Response.write YelpFeed.Get("phone") & "<br>" & vbNewLine
Response.write YelpFeed.Get("photo_url") & "<br>" & vbNewLine
Response.write YelpFeed.Get("review_count") & "<br>" & vbNewLine
End If
Next
Set RawResults = nothing
%>
I ended up using an intermediate page that grabbed the page from Yelp and outputted the JSON. Here is a snippet of code:
SET XmlObj = Server.CreateObject("Microsoft.XMLHTTP")
XmlObj.open "POST", url, FALSE
XmlObj.send
Response.write(XmlObj.responseText)
On my original page I used Ajax to grab the output from my intermediate page and then JavaScript to decode the JSON and output the results.
It works fine but I'm sure there's a better way.
精彩评论