views:

264

answers:

2

I'm trying to use Live Search 2.0 but even a simple example doesn't seem to work. Microsoft only has example code for 1.1 and they're not giving out AppIDs for that version.

Here's what I'm trying with:

<?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 is correctly specified in the code: I just erased it from here. I'm getting the following error:

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  A: 

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 )

You can also use the new JSON api with 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
    }

And finally theres a xml api you can look up at the msdn link as well and it can be fetched essentially the same way as the json you just need to decode it differently.

Marek
A: 

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

You may be aware that there are currently some issues with SOAP in PHP 5.2.6 - the Live Search service seems to be affected by it in both 1.1 and 2.0. The simplest workaround is to use another interface (JSON or XML)