Good news about Microsoft allowing access to their MSN Search service through SOAP ! It was definitely about time.
I've been trying to build a MSN Search API client with PHP. I'm using nuSOAP to interface with the SOAP server. However, I'm having a little trouble getting it to work. Just for the record, I've had developed several SOAP clients succesfuly (for example, using Google API).
This is the code that I'm using (of course, where it says 'This is my API key' I'm actually inserting a valid API key; also, the endpoint was taken out of the WSDL file):
define ('NUSOAP_PATH', dirname(__FILE__) . '/nusoap');
define ('MSN_API_ENDPOINT', 'http://soap.search.msn.com:80/webservices.asmx');
define ('MSN_API_KEY', 'This is my API key');
require_once(NUSOAP_PATH . '/nusoap.php');
$query = 'Example Query';
$parameters = array(
'AppID' => MSN_API_KEY,
'Query' => $query,
'CultureInfo' => 'en-US',
'Moderate' => 'Off',
'Requests' => array (
'SourceRequest' => array (
'Source' => 'Web',
'Offset' => 0,
'Count' => 10,
'ResultFields' => 'All'
)
)
);
$soapClient =& new soapclient(MSN_API_ENDPOINT);
$result =& $soapClient->call('Search', $parameters, 'http://schemas.microsoft.com/MSNSearch/2005/09/fex' );
if ($soapClient->getError())
{
echo '<b>ERROR</b>: ' . $soapClient->getError() . '<br />';
echo '<b>DEBUG</b>:<br /><pre>' . $soapClient->debug_str . '</pre>';
$result = false;
}
echo '<hr />';
echo '<pre>';
var_dump($result);
echo '</pre>';
What I'm getting is:
soapenv:Client: Client Error
as a SOAP error.
This is the XML request I send to the server:
< xml version="1.0" encoding="UTF-8" >
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd">
<SOAP-ENV:Body>
<ns1:Search xmlns:ns1="http://schemas.microsoft.com/MSNSearch/2005/09/fex">
<SearchRequest>
<AppID xsi:type="xsd:string">This is my API key</AppID>
<Query xsi:type="xsd:string">Example Query</Query>
<CultureInfo xsi:type="xsd:string">en-US</CultureInfo>
<Moderate xsi:type="xsd:string">Off</Moderate>
<Requests>
<SourceRequest>
<Source xsi:type="xsd:string">Web</Source>
<Offset xsi:type="xsd:int">0</Offset>
<Count xsi:type="xsd:int">10</Count>
<ResultFields xsi:type="xsd:string">All</ResultFields>
</SourceRequest>
</Requests>
</SearchRequest>
</ns1:Search>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And this is the XML response I get:
< xml version="1.0" encoding="utf-8" >
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Client</faultcode>
<faultstring>Client Error</faultstring>
<detail>Invalid request</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
So I'm getting an Invalid Request. I've tried changing the request several times with no luck. Anybody has a clue Can anybody give me an example XML request message for a succesful transaction, so I can base my code on that
Once I get it working I will post a PHP class that will handle the call to MSN Search API transparently for PHP applications.
Thanks !

Consuming MSN Search Web Service from PHP
bgs
You can see its sourcecode, and a link to a working version, in this post in my blog, entitled MSN Search API PHP Client.
xiang.zhiwei
I got my code working, but only for the first 10 results...anything else, and it won't work...it just regurgitates the first 10 results again when processing through my FOR logic ..apparently it's not passing the sourcerequest arrays....
i believe the problem is in the passing of the source request array, because it only send the default stuff whether i try to change to count field or i try to change the offset.
I know the rest of my code is tight....it works well for the google api....
any suggestions i can post the entire code if necessary...
Bow50
Compared to the google soap wsdl the msn search one seems overly complicated.
Also just another point, I have got a successful response back using when specifying the endpoint, but the returned document I cant get to be parsed by the simplexml extension. I havent had these problems with any other web service I have tried to work with.
Before anyone tells me to use non-WSDL mode as that works. I need to use WSDL mode so that I can utilise class mapping with the PHP 5 SOAP Extension.
pmpayne
Yes it does ! Good call quodlibet ! Enclosing the parameters as array ('Request' => $parameters) it was something that I've already tried, but I haven't also tried changing Moderate to SafeSearch. That was the trick.
I will shortly post the class here. Thanks for your help guys !
adrian44
$query = 'Example Query';
$parameters = array(
'AppID' => MSN_API_KEY,
'Query' => $query,
'CultureInfo' => 'en-US',
'SafeSearch' => 'Off',
'Requests' => array (
'SourceRequest' => array (
'Source' => 'Web',
'Offset' => 0,
'Count' => 10,
'ResultFields' => 'All'
)
)
);
$soapClient =& new soapclient(MSN_API_ENDPOINT);
$result =& $soapClient->call('Search', array("Request"=>$parameters), 'http://schemas.microsoft.com/MSNSearch/2005/09/fex' );
test it here : http://www.quodlibet.be/msnapi.php
Sang Kona
For the record I have tried the samples that come in the SDK from two locations today. Both times I received timeout errors. (I did put my APPID in the correct location.) I suspect there might be availability issues right now!
-David Sandor
Raja_Krish
Hi.
I'm trying to use MSN api, too.
I'm working on a port of the python API already made for Google : http://pygoogle.sourceforge.net/
Well. I'm too receiving the same Invalid request error, after sending, exactly (plus linebreaks for readability) :
BAD REQUEST :
< xml version="1.0" encoding="UTF-8" >
<soap-env:envelope soap-env:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<soap-env:body>
<ns1:search xmlns:ns1="http://schemas.microsoft.com/MSNSearch/2005/09/fex" soap-enc:root="1">
<request>
<query xsi:type="xsd:string">
test
</query>
<safesearch xsi:type="xsd:string">
Off
</safesearch>
<requests>
<sourcerequest>
<count xsi:type="xsd:int">
10
</count>
<source xsi:type="xsd:string">
Web
</source>
<resultfields xsi:type="xsd:string">
All
</resultfields>
<offset xsi:type="xsd:int">
0
</offset>
</sourcerequest>
</requests>
<cultureinfo xsi:type="xsd:string">
en-US
</cultureinfo>
<appid xsi:type="xsd:string">
1C53F41886E25BCA7D36F4D84B87B7B552178A0A
</appid>
</request>
</ns1:search>
</soap-env:body>
</soap-env:envelope>
Whereas this code, built from the PHP we're talking about, works perfectly well :
GOOD REQUEST
< xml version="1.0" encoding="ISO-8859-1" >
<soap-env:envelope soap-env:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/">
<soap-env:body>
<ns7540:search xmlns:ns7540="http://schemas.microsoft.com/MSNSearch/2005/09/fex">
<request>
<appid xsi:type="xsd:string">
1C53F41886E25BCA7D36F4D84B87B7B552178A0A
</appid>
<query xsi:type="xsd:string">
test
</query>
<cultureinfo xsi:type="xsd:string">
en-US
</cultureinfo>
<safesearch xsi:type="xsd:string">
Off
</safesearch>
<requests>
<sourcerequest>
<source xsi:type="xsd:string">
Web
</source>
<offset xsi:type="xsd:int">
0
</offset>
<count xsi:type="xsd:int">
10
</count>
<resultfields xsi:type="xsd:string">
All
</resultfields>
</sourcerequest>
</requests>
</request>
</ns7540:search>
</soap-env:body>
</soap-env:envelope>
The differences are :
- the encoding
- the year of xmlns:xsd and xmlns:xsi
- the soap-enc attribute in the <ns> tag (even when I remove it, I still get the error)
- the order of elements in the <request> tag
That's all. All and all. And I get an error.
Does anyone of you have the slightest idea of what's happening
Any help would be greatly appreciated.
ShivaP
now I'm wanting to expand what I'm using it for (ranking) and include the cache date, which is found in the DateTime tag. the problem that I would like to solve is being able to do both in the same query, but I can't seem to build the ResultFields filter correctly.
I've looked all over the net, but most scripts just use the all tag, which doesn't include the DateTime field. anybody tried to do this, or will I just have to suck it up and run two queries.
Ed
Graham Kingdon
Feel free to check it out and leave comments :)
http://www.onlinehoster.com/blog/msnlive-api-scraper-script/
SQLAdmin2005
<Request> instead of <SearchRequest>
Request is the object name, whereas SearchRequest is the object type.
<SafeSearch> instead of <Moderate>
Moderate is actually one of three values that the SafeSearch parameter can take. It's the default value.
The rest looks good. Hope that helps :).
--Navin
Saurav Sen
http://www.richardkmiller.com/files/msnsearch_nusoap.html (works)
http://www.richardkmiller.com/files/msnsearch_php5soap.html (doesn't work)
I posted a message to the PHP SOAP mailing list and one of the developers said it is a bug that he fixed in the latest release of PHP 5. Here is his message:
http://news.php.net/php.soap/2016
Hope this helps,
Richard
Royman
I am new to working with SOAP, where can i find more material regarding this
I hve PHP4 installed on my server.
free seo tips, guidelines