I've got a website which needs to send a request to another server and retrieve some data. I don't know anything about SOAP, so I need an expert help here.
Here is the data the second server is giving me.
I dont know where to start and how to do it. So any help is appreciated.
Could you give me a working example of php code which works with this code.
SOAP 1.1
The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
POST /Service1.asmx HTTP/1.1
Host: puanreport.retail.az
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/_find"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<_find xmlns="http://tempuri.org/">
<par>string</par>
</_find>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<_findResponse xmlns="http://tempuri.org/">
<_findResult>
<bon>
<Cari_Kod>string</Cari_Kod>
<Puan>string</Puan>
</bon>
<bon>
<Cari_Kod>string</Cari_Kod>
<Puan>string</Puan>
</bon>
</_findResult>
</_findResponse>
</soap:Body>
</soap:Envelope>
The most simple approach would be:
$client = new SoapClient('http://www.webservicex.net/geoipservice.asmx?WSDL');
$result = $client->GetGeoIP(array('IPAddress' => '8.8.8.8'));
print_r($result);
this gives you:
stdClass Object
(
[GetGeoIPResult] => stdClass Object
(
[ReturnCode] => 1
[IP] => 8.8.8.8
[ReturnCodeDetails] => Success
[CountryName] => United States
[CountryCode] => USA
)
)
Now you can simply access the values by invoking
$country = $result->GetGeoIPResult->CountryName;
Already answered here
php.net soap class
Related
How do you get the URI for using the SOAPClient which doesn't have the WSDL. The user has not provided us with the WSDL, but wanted to use their webservice using SOAP to get some information. I am looking for how to do it on PHP.
The confusion part is how to identify the URI value (target namespace). Based on their documentation,below is the sample they provided for SOAP call to me made.
The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.
**POST /webservices/wssamples/service.asmx HTTP/1.1
Host: www.samplecomponents.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length**
```
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<QueryAvailability xmlns="http://www.samplecomponents.com/webservices/">
<program_id>string</program_id>
<security_id>string</security_id>
<part_number>string</part_number>
</QueryAvailability>
</soap12:Body>
</soap12:Envelope>
```
**RESPONSE**
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<QueryAvailabilityResponse xmlns="http://www.samplecomponents.com/webservices/">
<QueryAvailabilityResult xmlns="http://www.samplecomponents.com/schemas/sample_availability.xsd">
<timestamp>dateTime</timestamp>
<item_count>unsignedInt</item_count>
<items>
<item>
<part_id>unsignedInt</part_id>
<manufacturer_part_number>string</manufacturer_part_number>
<stock_source>string</stock_source>
<digikey_part_number>string</digikey_part_number>
<description>string</description>
<quantity_available>unsignedInt</quantity_available>
<break_quantity>unsignedInt</break_quantity>
<rohs_compliant>bytes</rohs_compliant>
<flag_non_stock>boolean</flag_non_stock>
<flag_obsolete>boolean</flag_obsolete>
<flag_static_sensitive>boolean</flag_static_sensitive>
</item>
<item>
<part_id>unsignedInt</part_id>
<manufacturer_part_number>string</manufacturer_part_number>
<stock_source>string</stock_source>
<digikey_part_number>string</digikey_part_number>
<description>string</description>
<quantity_available>unsignedInt</quantity_available>
<break_quantity>unsignedInt</break_quantity>
<rohs_compliant>bytes</rohs_compliant>
<flag_non_stock>boolean</flag_non_stock>
<flag_obsolete>boolean</flag_obsolete>
<flag_static_sensitive>boolean</flag_static_sensitive>
</item>
</items>
</QueryAvailabilityResult>
</QueryAvailabilityResponse>
</soap12:Body>
</soap12:Envelope>
**HTTP POST**
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
POST /webservices/wssamples/service.asmx/QueryAvailability HTTP/1.1
Host: www.samplecomponents.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
program_id=string&security_id=string&part_number=string```
**RESPONSE**
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<sample_availability xmlns="http://www.samplecomponents.com/schemas/sample_availability.xsd">
<timestamp>dateTime</timestamp>
<item_count>unsignedInt</item_count>
<items>
<item>
<part_id>unsignedInt</part_id>
<manufacturer_part_number>string</manufacturer_part_number>
<stock_source>string</stock_source>
<digikey_part_number>string</digikey_part_number>
<description>string</description>
<quantity_available>unsignedInt</quantity_available>
<break_quantity>unsignedInt</break_quantity>
<rohs_compliant>bytes</rohs_compliant>
<flag_non_stock>boolean</flag_non_stock>
<flag_obsolete>boolean</flag_obsolete>
<flag_static_sensitive>boolean</flag_static_sensitive>
</item>
<item>
<part_id>unsignedInt</part_id>
<manufacturer_part_number>string</manufacturer_part_number>
<stock_source>string</stock_source>
<digikey_part_number>string</digikey_part_number>
<description>string</description>
<quantity_available>unsignedInt</quantity_available>
<break_quantity>unsignedInt</break_quantity>
<rohs_compliant>bytes</rohs_compliant>
<flag_non_stock>boolean</flag_non_stock>
<flag_obsolete>boolean</flag_obsolete>
<flag_static_sensitive>boolean</flag_static_sensitive>
</item>
</items>
</sample_availability>
These is no reference to URI or target namespace to be supplied on this documentation. Any help much appreciated.
The "target namespace" is simply the XML namespaces of the element inside the SOAP body - in this case the QueryAvailability and QueryAvailabilityResponse elements which would be the root of the document if all the SOAP wrapping wasn't there.
So you just need to read the xmlns attributes in the sample, which show those elements to be in the namespace http://www.samplecomponents.com/webservices/
our company has a .net base website
a few days ago we request a webservice from our website programmer and yesterday he sent me a URL
when i open the URL from localhost its shown a page that i can choose one of my four method (function) that we request to programming.
by choosing one of them a new page has boon shown as follow:
SOAP 1.1
The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
POST /webservices/findstatus.asmx HTTP/1.1 Host: localhost
Content-Type: text/xml; charset=utf-8 Content-Length: length
SOAPAction: "http://tempuri.org/FindWith_CodeR_Serail"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<FindWith_CodeR_Serail xmlns="http://tempuri.org/">
<CodeReception>int</CodeReception>
<Serial>string</Serial>
</FindWith_CodeR_Serail>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<FindWith_CodeR_SerailResponse xmlns="http://tempuri.org/">
<FindWith_CodeR_SerailResult>string</FindWith_CodeR_SerailResult>
</FindWith_CodeR_SerailResponse>
</soap:Body>
</soap:Envelope>
SOAP 1.2
The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.
POST /webservices/findstatus.asmx HTTP/1.1 Host: localhost
Content-Type: application/soap+xml; charset=utf-8 Content-Length:
length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<FindWith_CodeR_Serail xmlns="http://tempuri.org/">
<CodeReception>int</CodeReception>
<Serial>string</Serial>
</FindWith_CodeR_Serail>
</soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<FindWith_CodeR_SerailResponse xmlns="http://tempuri.org/">
<FindWith_CodeR_SerailResult>string</FindWith_CodeR_SerailResult>
</FindWith_CodeR_SerailResponse>
</soap12:Body>
</soap12:Envelope>
HTTP POST
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
POST /webservices/findstatus.asmx/FindWith_CodeR_Serail HTTP/1.1
Host: localhost Content-Type: application/x-www-form-urlencoded
Content-Length: length CodeReception=string&Serial=string
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>
Now i'm stock on php codes that call this web service with 2 parameter
please send me full code that i can paste it in a php file for test.
no matter what kind of method is used : SOAP or HTTP POST
please help me.
i used this php code and every thing is OK:
<?php
$client = new SoapClient("http://www.MyWebSite.com/webservices/findstatus.asmx?WSDL");
$params=new stdClass();
$params->CodeReception = '11111';
$params->Serial = '22222222';
$result = $client->FindWith_CodeR_Serail($params)->FindWith_CodeR_SerailResult;
echo $result;
?>
<?php
$client = new SoapClient("http://www.MyWebSite.com/webservices/findstatus.asmx?WSDL");
$params=new stdClass();
$params->CodeReception = '11111';
$params->Serial = '22222222';
$result = $client->FindWith_CodeR_Serail($params)->FindWith_CodeR_SerailResult;
echo $result;
?>
It's my first time using the SOAP API by BetFair I have created one XML file and one file that will call that file. I am not understanding how can I call this and get an output. I have created the XML file below:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<login xmlns="http://www.betfair.com/publicapi/v3/BFGlobalService/">
<request>
<locationId xmlns="">0</locationId>
<username xmlns="">usrname</username>
<password xmlns="">password</password>
<productId xmlns="">18</productId>
<vendorSoftwareId xmlns="">0</vendorSoftwareId>
</request>
</login>
</soap:Body>
</soap:Envelope>
Now to call this file I have also created one php file to do this.
BetFair has given this link for the login api: https://api.betfair.com/global/v3/BFGlobalService.wsdl
<?php
$get_data = file_get_contents("http://pixelonsell.com/dev2/betfair/login.xml");
$b = html_entity_decode($get_data);
$data= urlencode($b);
print_r($data);
$client = new SoapClient("https://api.betfair.com/global/v3/BFGlobalService.wsdl");
$result = $client->LoginReq($data);
print_r($result);
?>
I don't know why it's not working; can you help me out with this? Thank you in advance.
I would avoid using a dedicated SOAP library; in my experience they often don't work because of inconsistencies in the ways different servers implement the SOAP specification. Use a simple HTTP/HTTPS library instead, with the following headers
SOAPAction: urn:login
Content-Length: #{myContentLength}
Content-Type: text/xml
User-Agent: #{myUserAgent}
and the following payload (assuming you're using the Free API):
<SOAP-ENV:Envelope xmlns:SOAP-ENC="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">
<SOAP-ENV:Body>
<m:login xmlns:m="https://api.betfair.com/global/v3/BFGlobalService">
<m:request>
<username>#{myUsername}</username>
<password>#{myPassword}</password>
<locationId>0</locationId>
<vendorSoftwareId>0</vendorSoftwareId>
<productId>82</productId>
</m:request>
</m:login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
It looks to me like you're using the wrong namespace for the login element; should be 'https://api.betfair.com/..' not 'http://www.betfair.com/..'.
Good luck.
I'm a complete noob in XML and SOAP,
Could you give some advice on at least where to start, or some example?
(I'm not begging to write code for me)
Here are the specs:
I just expect to receive two double values. What is an easiest way to do it?
POST /CurrencyConvertor.asmx HTTP/1.1
Host: www.webservicex.net
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ConversionRate xmlns="http://www.webserviceX.NET/">
<FromCurrency>PHP</FromCurrency>
<ToCurrency>USD or EUR</ToCurrency>
</ConversionRate>
</soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ConversionRateResponse xmlns="http://www.webserviceX.NET/">
<ConversionRateResult>double</ConversionRateResult>
</ConversionRateResponse>
</soap12:Body>
</soap12:Envelope>
The above specs's origin: http://www.webservicex.net/CurrencyConvertor.asmx?op=ConversionRate
The first block is supposed to be a request, and the other response...
EDIT
Alright, I stopped at a standard PHP class, but I don't quite understand what is being asked for in this __doRequest method:
$client = new SoapClient();
$client->__doRequest ( <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ConversionRate xmlns="http://www.webserviceX.NET/">
<FromCurrency>PHP</FromCurrency>
<ToCurrency>USD or EUR</ToCurrency>
</ConversionRate>
</soap12:Body>
</soap12:Envelope>
EOD
, "http://www.webservicex.net/CurrencyConvertor.asmx" , $???, $??? );
http://www.php.net/manual/en/soapclient.dorequest.php
What is action, and what should I put as version, I know - a soap version 1.2 but the parameter is int so it cannot be assigned a 1.2 value lol...
EDIT2:
Alright, this is what I've got so far, but it gets me an empty string...
$client = new SoapClient(null, array('location'=>'http://www.webservicex.net/CurrencyConvertor.asmx','uri'=>''));
$client->__doRequest ( <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ConversionRate xmlns="http://www.webserviceX.NET/">
<FromCurrency>PHP</FromCurrency>
<ToCurrency>USD or EUR</ToCurrency>
</ConversionRate>
</soap12:Body>
</soap12:Envelope>
EOD
, "http://www.webservicex.net/CurrencyConvertor.asmx" , "ConversionRate", 2 );
echo "Response :<br>", htmlentities($client->__getLastResponse());
...I just dont get this 'uri' thing - beyond my understanding. It makes me want to hit my head to the wall.
Basically what you need are two parts.
A SOAPClient class, which solves the communication, knows about the server URL, sends requests and receives responses (which you got in XML in your question) and also triggers #2, which is:
The second part is an XML parser / marshaller, which can convert a request objects (containing fromCurrency and toCurrency) to a correct XML string the server can understand (according to the WSDL) and convert some XML into a response object again using the WSDL. This part is a bit tricky, but I found a lot of documentation for that, search for "SOAP PHP" and you get some examples). These two processes are called marshalling and unmarshalling (to help you find something quicker with Google). Your StockQuote webservice doesn't really provide a WSDL, which is basically a description of all possible operations (in your case just one: GetQuote) and its available objects (in your case only simple types string, which don't have to be defined, since they are WSDL standard)
I haven't done SOAP with PHP, but spent a lot of time with Java+SOAP and can understand how difficult it is. In theory you just send some XML to the server and get some XML back. But the XML sent has to be in the correct format and when you receive a response, you want to convert the response into an object and not deal with some XML string.
For a simple web service like this, you might consider constructing the XML request by hand (simply putting together the XML string) and substr the response to find the requested answer.
There is a webservice (WSDL) with a lot of functions. I want to call one of these functions with PHP.
The webservice provides a documentation wherein they put a format including header and xml, but I have no idea how I need to send the request from PHP. I searched for a couple of hours now, and I simply don't know.
An example request they provided:
POST POSTURL HTTP/1.1
Host: HOST
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "SOAPLOCATION"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Version xmlns="URL" />
</soap:Body>
</soap:Envelope>
The response I should receive is also given, but I can't even send the request.
How do I send a request with PHP to get the response?
I tried some PHP SoapClient things, but I can't find a easy to read tutorial or some clear explanation...
If anyone can help me, that would be great!
Mapping some typical information may look something like this.
$data = array(
'UserName' => $user->getUsername(),
'Password' => $user->getPassword(),
'Email' => $user->getEmail(),
'FirstName' => $user->getFirstName(),
'LastName' => $user->getLastName(),
);
$response = $this->getDatasource()->TheServiceMethodForCreatingAUser(
array(
'user' => $data
)
);
The response is then handled however you wish (via an entity or response object of some description). The header must be done seperately by using new SoapHeader().
Hope this helps.
I've an addition to the question:
The request is as following:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns="...">
<s:Body>
<Search>
<login>
<Wholesaler>...</Wholesaler>
<Username>...</Username>
<Password>...</Password>
</login>
<itemRequests>
<ItemRequest>
<ArticleId>int</ArticleId>
<SupplierId>int</SupplierId>
<QuantityRequested>int</QuantityRequested>
</ItemRequest>
</itemRequests>
</Search>
</s:Body>
</s:Envelope>
There are two SoapHeaders I need to send:
Content-Type: text/xml; charset=utf-8
SOAPAction: URI
I do know the service provider and the action identifier.
If I have the following variables
$service
$action
$request
$header
How can I send the request in PHP?
I tried
$client = new SoapClient($service);
$result = $client->__doRequest($request, $service, $action);
But I do not receive a response it seems...
This is what the response should like:
Date: Thu, 09 Aug 2012 08:01:40 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Content-Length: 408
Cache-Control: private
Content-Type: text/xml; charset=utf-8
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SearchResponse xmlns="...">
<SearchResult>
<Wholesaler>...</Wholesaler>
<Username>...</Username>
<Error>false</Error>
<DateTime>2012-08-09T10:01:40.39125+02:00</DateTime>
<ItemResults />
</SearchResult>
</SearchResponse>
</s:Body>
</s:Envelope>
When I do a simple echo $result, the screen stays white and in the code there is no XML visible.