English 中文(简体)
PHP5和微软Live Search 2.0
原标题:
  • 时间:2008-12-17 15:43:43
  •  标签:

我正在尝试使用Live Search 2.0,但即使是简单的示例似乎也无法工作。微软仅提供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
最佳回答

You could begin by using the proper soap api url for 2.0. It s now "http://api.search.live.net/search.wsdl?AppID=YourAppId" taken from (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
    } 

最后,您还可以查看MSDN链接中的XML API,它可以基本上与JSON以相同的方式获取,只需要以不同的方式进行解码即可。

问题回答

The sample code for API 2.0 is on MSDN but we do not have the complete PHP code sample out yet. A first code sample (very similar to the one in the answer you already got) in included in the blog post on the Live Search Developer Blog

您可能已经注意到PHP 5.2.6中当前存在一些SOAP问题 - Live Search服务似乎受到1.1和2.0的影响。 最简单的解决方法是使用另一个接口(JSON或XML)。





相关问题
热门标签