Bing API 开发地址:http://cn.bing.com/developers
示例一:拼写检查
function DoSpellCheck(text: string): string;
var
xmldoc: TXMLDocument;
inode,mnode,rnode,irnode: IXMLNode;
j: integer;
uri: string;
webcopy: TWebCopy;
begin
Result := '';
webcopy := TWebCopy.Create(application);
try
with webcopy.Items.Add do
begin
url := 'http://api.bing.net/xml.aspx?Appid=' + AppID + '&query='+HTTPEncode(text)+
'&Sources=Spell&Version=2.0&Market=en-us&Options=EnableHighlighting';
TargetFilename := 'response.xml';
Protocol := wpHttp;
end;
webcopy.ShowDialog := false;
webcopy.Execute;
finally
webcopy.Free;
end;
xmldoc := TXMLDocument.Create(application);
try
xmldoc.LoadFromFile('response.xml');
inode := xmldoc.ChildNodes.FindNode('SearchResponse');
if Assigned(inode) then
begin
uri := 'http://schemas.microsoft.com/LiveSearch/2008/04/XML/spell';
mnode := inode.ChildNodes.FindNode('Spell',uri);
if Assigned(mnode) then
begin
rnode := mnode.ChildNodes.FindNode('Results',uri);
if Assigned(rnode) then
begin
irnode := rnode.ChildNodes.FindNode('SpellResult',uri);
if Assigned(irnode) then
Result := irnode.ChildNodes.FindNode('Value',uri).NodeValue;
end;
end;
end;
finally
xmldoc.Free;
end;
end;
begin
// sample call with a forced spelling error
ShowMessage( DoSpellCheck('Mispeling words is a common ocurrence') );
end;
示例二:获取图片链接
function GetImageLink(searchstr: string): string;
var
xmldoc: TXMLDocument;
inode,mnode,rnode,irnode: IXMLNode;
j: integer;
uri: string;
webcopy: TWebCopy;
begin
Result := '';
webcopy := TWebCopy.Create(application);
try
with webcopy.Items.Add do
begin
url := 'http://api.bing.net/xml.aspx?Appid=' + AppID + '&query='+HTTPEncode(searchstr)+'&sources=image&image.count=1';
TargetFilename := 'response.xml';
Protocol := wpHttp;
end;
webcopy.ShowDialog := false;
webcopy.Execute;
finally
webcopy.Free;
end;
xmldoc := TXMLDocument.Create(application);
try
xmldoc.LoadFromFile('response.xml');
inode := xmldoc.ChildNodes.FindNode('SearchResponse');
if Assigned(inode) then
begin
uri := 'http://schemas.microsoft.com/LiveSearch/2008/04/XML/multimedia';
mnode := inode.ChildNodes.FindNode('Image',uri);
if Assigned(mnode) then
begin
rnode := mnode.ChildNodes.FindNode('Results',uri);
if Assigned(rnode) then
begin
irnode := rnode.ChildNodes.Nodes[0];
if Assigned(irnode) then
Result := irnode.ChildNodes.FindNode('MediaUrl',uri).NodeValue;
end;
end;
end;
finally
xmldoc.Free;
end;
end;
begin
// sample retrieving an image hyperlink and show it on the form using a TWebImage component
imageurl := GetImageLink('mercedes SL gullwing');
caption := imageurl;
Screen.Cursor := crHourGlass;
WebImage1.URL := imageurl;
Screen.Cursor := crDefault;
end;