Using Bing API SOAP + PHP

Bing API Library Preview of 1.2It has become quite an annoyance to make a simple support for Bing API SOAP. Honestly for PHP I would say you should stick with either JSON or XML. Of course my personal choice would be XML since I find it more flexible, then again, whatever floats your boat :)

That to say this is probably my first time using SOAP while I find no benefit using it on Bing’s SOAP Server it’s really educational in a way to get to know how it’s done. I don’t know if there is any benefit actually, if someone cares to fill me in drop your thoughts in a comment. Some side-thoughts is that the response from Bing’s SOAP Server is way more clear than the response given in XML, maybe.

In this post I’m going to be showing you how to make a request to Bing’s SOAP Server using SOAPClient object.

Let’s give this a try, create a php file and name it whatever you want.

try {
            $client = new SoapClient("http://api.bing.com/search.wsdl", array(
                'features', SOAP_USE_XSI_ARRAY_TYPE,
                'style'    => SOAP_DOCUMENT,
                'use'      => SOAP_LITERAL,
                'soap_version'   => SOAP_1_2,
                'trace'=>true,
                'exceptions'=>true));
            if(!$client) {
                throw new SoapFault('You either have no SOAP support in your PHP or there is a problem contacting the server.');
            }
        } catch(SoapFault $ex) {
            echo $ex->getCode();
            echo $ex->getMessage();
            echo $ex->getLine();
            echo $ex->getTrace();
            echo "Request made:\n" . $client->__getLastRequestHeaders() . "\n";
            echo "Response:\n" . $client->__getLastResponse() . "\n";
            echo "Response containing headers:\n" . $client->__getLastResponseHeaders() . "\n";
        }

Run it, while of course there will be no output, unless it runs into the Exception. Everything will be fine if it doesn’t do both.

Now we have a variable called $client that contains the object of SOAPClient. Let’s make a call to the server. Add this below in your php file.

try {
    $parameters = array(
            'SearchRequest' =>
                    array('parameters' =>
                                array('Version' => '2.0',
                                      'Market' => 'en-us',
                                      'Query' => 'Bing API PHP Library',
                                      'AppId' => 'YOUR APPID KEY',
                                      'Sources' =>
                                             array(
                                                'SourceType' => 'Web'),
                                      'Web' => array(
                                                  'Count' => '50',
                                                  'Offset' => '10')
                                )
                     )
         );
 $s = $client->__soapCall('Search', $parameters  );
    if(!$s) {
    throw new SoapFault();

} } catch(SoapFault $ex) {

    echo $ex->getCode();
    echo $ex->getMessage();
    echo $ex->getLine();
    echo $ex->getTrace();

    echo "Response:\n" . $client->__getLastResponse() . "\n";
    echo "Tne response headers:\n" . $client->__getLastResponseHeaders() . "\n";
}

$xmlResponse = $client->__getLastResponse();
var_dump($xmlResponse);

Now you will know why I don’t like using the SOAP solution right now. It’s because even if it gives you a much clean response you will have to write quite a lot of arrays just to get the results you want. I say that’s one of the “default” ways to make the request to SOAP Server.

If everything is done correctly then you will see a dump of XML with the results. Else it will catch the exception. I think it’s worth mentioning that you will need to learn the structure of BING SOAP Server, it’s not hard but quite bothersome. Get the types using

var_dump($client->__getTypes());

You will see a wide choice of options. But you will start with struct SearchRequest and learn what’s in the brackets. Like Alessandro Catorcini (BING API Lead Program Manager) have said to me in a e-mail reply. It’s very straightforward and if you sit down and see what we have on the plate it’s really easy to figure out.

Another word, if you notice getTypes output and our parameters made, both of them are SIMILAR in every way. How so? getTypes HELPS YOU on how you should interact with the server and tells you how will be the structure of the request nad how it will be outputted. It’s essential that you get familiar with the Types.

Hope you found this useful, that, and to let you guys know that the implementation in Bing API Library is already on its way.

7 people like this post.
This entry was posted in PHP, Programming. Bookmark the permalink.

4 Responses to Using Bing API SOAP + PHP

  1. Randeep says:

    Hi David

    Thanks For your Blog

    I am very much new in Bing APIs. But I got a project to customize Bing Map as per the Data given ( which contains a variety of data on a “Point of Interest” things like a Hotel, Shop, Pub, School etc. )

    Kindly give me some idea about using APIs to customize BING Map using PHP

    Looking forward u’r reply

    • David says:

      Hello Randeep,

      Sadly I haven’t looked deep enough on BING Map API, I don’t know if I’m correct but it’s all JavaScript which is not my cup of tea. I’ll try to look onto it.

  2. Kevin Fry says:

    The only problem I’m having is the xml responses having the tags. Url I can deal with no problems, but by them putting the “web:” in there, it’s throwing my parser off. Argh, I’ll figure it out sooner or later.

  3. Ken says:

    Hi david, soap also become my favorite way too, to make simple query on such webservice, thanks for the code….

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>