index > Windows Live Search: Development > Consuming MSN Search Web Service from PHP

Consuming MSN Search Web Service from PHP

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 !



http://www.marianoiglesias.com.ar
Mariano Iglesias
Mariano,

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


Mark the best replies as answers!
dsandor
The XML request should have the following changes:

<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


Developer, MSN Search
Navin Joy
    This version works


$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



insanity is a fulltime job
quodlibet
 quodlibet wrote:
    This version works
test it here : http://www.quodlibet.be/msnapi.php


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 !



http://www.marianoiglesias.com.ar
Mariano Iglesias
As promised, I just created the class to interact with MSN Search API with PHP.

You can see its sourcecode, and a link to a working version, in this post in my blog, entitled MSN Search API PHP Client.



http://www.marianoiglesias.com.ar
Mariano Iglesias
Is there any way to use the MSNSearch web service using the WSDL file, currently I cant get either NuSOAP or the PHP 5 SOAP Extension to work using only the WSDL. To use any of them I have to specify the endpoint for the service, something which the WSDL file is supposed to handle. As well as the setting of the namespace.

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.
Geoff Garside
(Sorry for the formatting, I was unable to find any help on this forum.)

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.

lagroue
Hello, i've gotten php5 to connect to the api, and I'm now trying to write a simple ranking script...
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....
$request
Edwin Jeffcoat
I ran into this problem too but it turned out to be a problem with PHP SOAP not MSN.  (If you run a packet sniffer on the outbound traffic, you'll see that it's actually asking for 10 results instead of 50.)  NuSOAP, on the other hand, works.

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
Richard Miller
got all that working...(actually some time ago)

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



Ed
Edwin Jeffcoat
reply 11

You can use google to search for other answers

 

More Articles

• Msn Web services ?
• org.apache.axis.ConfigurationException: java.lang.IllegalStateExc...
• How to change a CultureInfo and what effect on search it has!?
• How can I turn on Child Search with MSN Search Query Parameters l...
• message
• I am not able to sign in on MSN messenger Please help anyone!
• Using MSN Search from aspx page - Questions on getting started
• Results Not All There
• Using Search API to return adverts
• How can i get Account and TOKEN for WEB services
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• MSN SEARCH SUCKS!
• proxy authentication required error
• Results Not All There
• Access RunTime installations
• optional parameters for xml
• Image search?
• I am not able to sign in on MSN messenge
• Windows Live Search API (Needs to be Sim
• Web Server Extensions
• Welcome to the MSN Search Web Service Fo
• not well-formed (invalid token) error
• spelling with CultureInfo = "de-De&
• "Client Error"
• Windows Live: IE7 Search Bar
• How can i get Account and TOKEN for WEB

Hot Articles

• Access RunTime installations
• Error while using MSN Search Web Service
• MSN Search Web Service SDK Version 0.60
• Limiting results to one domain or a set
• Showing non-english caracters in search
• Yahoo API
• What consitutes a query?
• Parsing SearchTags
• No results with inurl: parameter
• Setting multiple cultureinfo
• Results Ranking
• Keywords
• Local Search Issues
• DateTime field
• Python Interfacing with API

Recommend Articles

• Is msn search api good for my purpose?
• Search News?
• Keywords
• Setting multiple cultureinfo
• Client Error
• ApplicationId-Ip Specific
• Results Not All There
• spanish spelling correction
• application ids
• Use asynchronous Search API
• Consuming MSN Search Web Service from PHP
• Search is returning only 25 results
• Windows Live Search API (Needs to be Sim
• live.com too slow
• escaped XML string ---> XML?