|
|
Add Google Search to Windows-1256 Arabic ASP Page
ASP example code showing how to use the Google Search API and convert the utf-8 XML response to Windows-1256 for display on a Arabic ASP page.
<%@ LANGUAGE="VBSCRIPT" %>
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=Windows-1256">
<title>Sample Arabic Google Search</title>
</head>
<body bgcolor="#FFFFFF">
<%
Dim GoogleUrl,GoogleKey,SoapText,ObjXml,UnicodeResponse,CXml
' Get a key from Google...
GoogleKey = "00000000000000000000000000000000"
' Create a SOAP message for a Google Search Request
' The language is set to Arabic
SoapText = "<?xml version='1.0' encoding='UTF-8'?>" & _
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" " & _
"xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" " & _
"xmlns:xsd=""http://www.w3.org/1999/XMLSchema"">" & _
" <SOAP-ENV:Body>" & _
" <ns1:doGoogleSearch xmlns:ns1=""urn:GoogleSearch"" " & _
" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">" & _
" <key xsi:type=""xsd:string"">"&GoogleKey&"</key>" & _
" <q xsi:type=""xsd:string"">Arabic newspaper</q>" & _
" <start xsi:type=""xsd:int"">0</start>" & _
" <maxResults xsi:type=""xsd:int"">10</maxResults>" & _
" <filter xsi:type=""xsd:boolean"">true</filter>" & _
" <restrict xsi:type=""xsd:string""></restrict>" & _
" <safeSearch xsi:type=""xsd:boolean"">false</safeSearch>" & _
" <lr xsi:type=""xsd:string"">lang_ar</lr>" & _
" <ie xsi:type=""xsd:string"">latin1</ie>" & _
" <oe xsi:type=""xsd:string"">latin1</oe>" & _
" </ns1:doGoogleSearch>" & _
" </SOAP-ENV:Body>" & _
"</SOAP-ENV:Envelope>"
GoogleUrl = "http://api.google.com/search/beta2"
' Make the SOAP Google API Call
Set ObjXml = CreateObject("Microsoft.XMLHTTP")
ObjXml.open "POST",GoogleUrl,"False"
ObjXml.setRequestHeader "Man", "POST"+" "+GoogleUrl+" HTTP/1.1"
ObjXml.setRequestHeader "MessageType", "CALL"
ObjXml.setRequestHeader "Content-Type", "text/xml"
ObjXml.send SoapText
' Get the Google Search Response
' Google requests and responses are always utf-8.
' However, when the Microsoft.XMLHTTP object returns the response
' text, it is already converted to a Unicode string.
UnicodeResponse = objXML.responseText
Set ObjXml = Nothing
' Create a Chilkat ASP XML object for easy parsing.
' ASP XML is a free ASP component from Chilkat Software.
set CXml = Server.CreateObject("Chilkat_9_5_0.Xml")
CXml.LoadXml UnicodeResponse
' This page requires Windows-1256
' Create a Chilkat Charset Conversion component for conversion from Unicode
' The Chilkat Charset component is not a free component, and can
' be licenced from Chilkat Software, Inc.
set cc = Server.CreateObject("Chilkat_9_5_0.Charset2")
' Any value passed to UnlockComponent begins the 30-day trial.
cc.UnlockComponent "30-day trial"
cc.ToCharset = "Windows-1256"
' Quickly navigate to the result elements.
set resultElements = CXml.SearchForTag(Nothing,"resultElements")
if not (resultElements is nothing) then
' Find the first item
set item = resultElements.SearchForTag(Nothing,"item")
while not (item is nothing)
Response.Write "Title: "
title = item.GetChildContent("title")
if Len(title) > 0 then
Response.BinaryWrite cc.ConvertFromUnicode(title)
end if
Response.Write "<br>"
Response.Write "URL: " & item.GetChildContent("URL") & "<br>"
Response.Write "Summary: "
summary = item.GetChildContent("summary")
if Len(summary) > 0 then
Response.BinaryWrite cc.ConvertFromUnicode(summary)
end if
Response.Write "<br><br>"
set item = item.NextSibling()
wend
else
Response.Write CXml.GetXml()
end if
%>
</body>
</html>
|