HartleySan
HartleySan

Reputation: 7810

How do I connect to and make a request to the following sample SOAP service in PHP?

It's just come to my attention that I'm going to have to interact with a SOAP API in the near future. Up until now though, I've only ever used REST APIs and never a SOAP API.

I've been reading about SOAP APIs and thus far my (very likely wrong) understanding is as follows:

I have two questions:

  1. Is my understanding of SOAP above is even correct to begin with?
  2. How do I use PHP to connect and make requests to the following sample SOAP API: http://www.webservicex.com/globalweather.asmx?op=GetWeather

(If the sample SOAP API linked above is not a very good one, then I'm open to learning how to use SOAP with any other sample SOAP APIs that are available.)


Edit: pawelwaw, I wrote the following sample code to test everything out, but it seems like no matter what value I use for CityName in the GetWeather operation, I get no data back. Is my understanding incorrect, or is the SOAP API just not very good?

<?php

  $client = new SoapClient('http://www.webservicex.com/globalweather.asmx?wsdl');

  echo '<h2>Types:</h2>';
  echo '<pre>';
    var_dump($client->__getTypes());
  echo '</pre>';

  echo '<h2>Functions:</h2>';
  echo '<pre>';
    var_dump($client->__getFunctions());
  echo '</pre>';

  echo '<h2>GetCitiesByCountry:</h2>';
  echo '<pre>';
    echo htmlentities($client->GetCitiesByCountry([
      'CountryName' => 'Poland'
    ])->GetCitiesByCountryResult);
  echo '</pre>';

  echo '<h2>GetWeather:</h2>';
  echo '<pre>';  
    var_dump($client->GetWeather([
      'CityName' => 'Krakow',
      'CountryName' => 'Poland'
    ]));
  echo '</pre>';

Upvotes: 0

Views: 1033

Answers (1)

pawelwaw
pawelwaw

Reputation: 52

I ussally use SOAP api. And for this service You can use this sample code to get Cities of Country

<?php
  $api = new SoapClient ( 'http://www.webservicex.com/globalweather.asmx?WSDL' );
  $res = $api->GetCitiesByCountry(array( 'CountryName' =>"Poland"));

  var_dump($res);
?>

then you are able to run 2nd method of this api to get Weather http://www.webservicex.com/globalweather.asmx?op=GetWeather

I hope, it helps.

It seems that function GetWeather ( http://www.webservicex.com/globalweather.asmx?op=GetWeather ) don't work correctly. when I filled values manually on site then there is no response with data, but for function GetCitiesByCountry ( http://www.webservicex.com/globalweather.asmx?op=GetCitiesByCountry ) returns data. I hadn't many expirience with API, but this should work, in my opinion there is a problem with this function in SOAP, because don't work via http also. I tested it via REST, and this function don't work according to specification.

Upvotes: 1

Related Questions