It 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.
1 people like this post.
Like 