Recently we have received a couple of projects in which we consume webservices SOAP for data. The truth is that it was a while since I saw them in my usual work. A few years ago were very common for just about anything that would involve communication with external sources, however recent years had fallen into disuse for some simple things complicated since quite a job with a simple HTTP request and basic XML could be resolved . Precisely this has always been one of the main criticisms of the protocol SOAP , high bandwidth consumption for a simple request.

Until now had always used nusoap to make SOAP calls from PHP , however I ran into a problem when accessing a service of an important means of international communication. The call nusoap not returning results while from html test system everything worked properly.

After giving many turns and did not find any errors (other requests to the same service itself that worked) I realized that PHP already has a set of SOAP functions native, so no need nusoap. The question was would it work well, as it were. After adding the appropriate extension arises a small inconsistency. Nusoap can not use native and you run the same installation of PHP. Put another way, if you enable the SOAP extension, nusoap stop working and start throwing error messages because many of the functions you use have the same name as the native, which would then be reserved names. If you have any application that uses nusoap on the same machine also have to migrate it to use native functions.

Today's example is a simple call to a service that returns a list of news.

The process is very simple, you need the url of the webservice, the method that you call and the parameters to pass and, as in any SOAP service will return XML.

  1. ; //url del servicio $ Service = "http://dominio.com/noticias?wsdl" / / url of the service
  2. ( ) ; //parametros de la llamada $ Parameters = array () / / parameters of the call
  3. 'idioma' ] = "es" ; $ Parameters ['language'] = "is";
  4. 'usuario' ] = "manolo" ; $ Parameters ['user'] = "manolo";
  5. 'clave' ] = "tuclave" ; $client = new SoapClient ( $servicio , $parametros ) ; $ Parameters ['key'] = "tuclave"; $ client = new SoapClient ($ service, $ parameters);
  6. -> getNoticias ( $parametros ) ; //llamamos al métdo que nos interesa con los parámetros $ Result = $ client -> getNoticias ($ parameters) / / call the metdo that interests us the parameters

With these simple guidelines we have in $ result the result XML service call. How to work with XML is a bit cumbersome, convert it to an associative array so that we make it easier to process the data, we use the function obj2array I indicate below.

  1. $result ) ; $ Result = obj2array ($ result);
  2. [ 'resultado' ] [ 'noticias' ] ; $ News = $ result ['result'] ['news'];
  3. ( $noticias ) ; $ N = count ($ news);
  4. / / Process the result as any other array
  5. $i = 0 ; $i < $n ; $i ++ ) { for ($ i = 0, $ i <$ n, $ i + +) {
  6. [ $i ] ; $ News = $ news [$ i];
  7. [ 'id' ] ; $ Id = $ news ['id'];
  8. / / Here go the rest of your code where you process the data received
  9. }
  10. $obj ) { obj2array function ($ obj) {
  11. ( ) ; $ Out = array ();
  12. $obj as $key => $val ) { foreach ($ obj as $ key => $ val) {
  13. true ) { switch (true) {
  14. ( $val ) : case is_object ($ val):
  15. $key ] = obj2array ( $val ) ; $ Out [$ key] = obj2array ($ val);
  16. break;
  17. ( $val ) : case is_array ($ val):
  18. $key ] = obj2array ( $val ) ; $ Out [$ key] = obj2array ($ val);
  19. break;
  20. default:
  21. $key ] = $val ; $ Out [$ key] = $ val;
  22. }
  23. }
  24. ; return $ out;
  25. }

In the second line we keep the array elements that we want to process. If you do not know what your webservice returns can do a var_dump ($ result) and see all the results. In our case, as is a sequence of news, we are left with the item that has the news.

As you may have noticed, I have not worried error handling when calling the webservice. That I leave as an exercise to you, it's August and I do not want icon razz Consumiendo webservices SOAP desde PHP webservice soap PHP . In Manual of PHP is all the information.

And that's all folks, today was a simple example but very useful when you need to use SOAP.