PHP5和Microsoft Live Search 2.0

时间:2008-12-17 15:43:43

标签: php soap livesearch

我正在尝试使用Live Search 2.0,但即使是一个简单的示例也似乎不起作用。 Microsoft只有1.1的示例代码,并且他们没有为该版本提供AppID。

以下是我正在尝试的内容:

<?php
$server = new SoapClient('http://soap.search.msn.com/webservices.asmx?wsdl');

class Search {
    public $Request;
}

class SearchRequest {
    public $AppID;
    public $Query;
    public $CultureInfo;
    public $SafeSearch;
    public $Flags;
    public $Location;
    public $Requests;
}

class SourceRequest {
    public $Source;
    public $Offset;
    public $Count;
    public $FileType;
    public $SortBy;
    public $ResultFields;
    public $SearchTagFilters;
}

$searchRequest = new SourceRequest();
$searchRequest->Source = 'Web';
$searchRequest->Offset = 0;
$searchRequest->Count = 5;
$searchRequest->ResultFields = 'All SearchTagsArray';

$request = new SearchRequest();
$request->AppID = '...';
$request->Query = 'Bill Gates';
$request->CultureInfo = 'en-US';
$request->SafeSearch = 'Off';
$request->Flags = '';
$request->Requests = array($searchRequest);

$search = new Search();
$search->Request = $request;

$server->Search($search);
?>

在代码中正确指定了AppID:我刚从这里删除它。我收到以下错误:

Array ( [0] => SearchResponse Search(Search $parameters) )
Fatal error: Uncaught SoapFault exception: [soapenv:Client] Client Error in /Users/thardas/Sites/vt9/widgets/ms_livesearch.php:41
Stack trace:
#0 [internal function]: SoapClient->__call('Search', Array)
#1 /Users/thardas/Sites/vt9/widgets/ms_livesearch.php(41): SoapClient->Search(Object(SearchRequest))
#2 /Users/thardas/Sites/vt9/index.php(23): include('/Users/thardas/...')
#3 {main} thrown in /Users/thardas/Sites/vt9/widgets/ms_livesearch.php on line 41

2 个答案:

答案 0 :(得分:2)

你可以从适当的肥皂api url开始使用2.0。 下雪了 “http://api.search.live.net/search.wsdl?AppID=YourAppId”来自(http://msdn.microsoft.com/en-us/library/dd250965.aspx

你也可以使用新的JSON api和php。

$appid = 'Your app id';
$searchitem = 'PHP Manual';
    $request = 'http://api.search.live.net/json.aspx?Appid=' . $appid . '&sources=web&query=' . urlencode( $searchitem);
    $response  = file_get_contents($request);
    $jsonobj  = json_decode($response);
    foreach($jsonobj->SearchResponse->Web->Results as $value)
    {
    //$value->Url
    //$value->Title
    //$value->Description
    } 

最后还有一个xml api,你也可以查看msdn链接,它的获取方式基本上与你需要以不同方式解码它的json相同。

答案 1 :(得分:0)

API 2.0的示例代码在MSDN上,但我们还没有完整的PHP代码示例。 博客文章on the Live Search Developer Blog中包含的第一个代码示例(非常类似于您已经得到的答案中的代码示例)

您可能知道PHP 5.2.6中当前有一些issues with SOAP - Live Search服务似乎在1.1和2.0中都受到它的影响。最简单的解决方法是使用另一个接口(JSON或XML)