SOAP response by given request and response structure in PHP - php

As I am pretty new to SOAP I really need some help to get started. I have been given this structur:
request:
POST /NumbriParing/NumbriParing.asmx HTTP/1.1
Host: nba.tja.ee
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://nba.sa.ee/NumriomanikuParing"
<?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>
<NumriomanikuParing xmlns="http://nba.sa.ee/">
<number>long</number>
</NumriomanikuParing>
</soap:Body>
</soap:Envelope>
Response:
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>
<NumriomanikuParingResponse xmlns="http://nba.sa.ee/">
<NumriomanikuParingResult>
<Number>long</Number>
<OmanikuRegNumber>string</OmanikuRegNumber>
<Omanik>string</Omanik>
<VastusKood>NumberLeitud or NumbritEiLeitud</VastusKood>
</NumriomanikuParingResult>
</NumriomanikuParingResponse>
</soap:Body>
</soap:Envelope>
I need to replace the "long" placeholder in the request with a numeric variable to get the request for that number.
The asmx is located in https://nba.tja.ee/NumbriParing/NumbriParing.asmx
How Can it be done using php?
Best regards,
Martti

Use SoapClient and WSDL of the service you need.
Something like:
try {
$client = new SoapClient('https://nba.tja.ee/NumbriParing/NumbriParing.asmx?WSDL');
$param = new stdClass();
$param->number = 123;
$result = $client->NumriomanikuParing($param);
var_dump($result);
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}

Related

PHP function SOAP 1.2

I need some help to execute a SOAP request with PHP.
For example, a method for search a publisher:
POST /webservice/search.asmx HTTP/1.1
Host: www.host.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:soapenc="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://host.com/HostNET/" xmlns:types="http://host.com/HostNET/encodedTypes" xmlns:rpc="http://www.w3.org/2003/05/soap-rpc" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body soap12:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<tns:findPublishers>
<sessionToken xsi:type="xsd:string">string</sessionToken>
<expression soapenc:id="id0" xsi:type="tns:SearchExpression">
<expression xsi:type="xsd:string">string</expression>
<phonetic xsi:type="xsd:boolean">boolean</phonetic>
<inflections xsi:type="xsd:boolean">boolean</inflections>
</expression>
<distributeurs xsi:type="xsd:boolean">boolean</distributeurs>
</tns:findPublishers>
</soap12:Body>
</soap12:Envelope>
I have the sessionToken but I don't understand what I have to do with the line <expression soapenc:id="id0" xsi:type="tns:SearchExpression"> and it's contents.
try{
$search = new SoapClient("http://www.host.com/WebService/search.asmx?wsdl", array("soap_version" => SOAP_1_2));
$search->findPublishers($sessionToken,???????,FALSE);
}catch (SoapFault $e){
print_r($e);
}

php soap client request

How to send PHP SOAP Request to .NET Web Services, I have already tried soapclient(); class but something wrong..... any body please help me.
POST /KH/TestKH/WebServices.asmx HTTP/1.1
Host: www.host.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/SearchMethod"
<?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>
<SearchMethod xmlns="http://tempuri.org/">
<_hap>string</_hap>
<_hapType>string</_hapType>
<UserID>string</UserID>
<Password>string</Password>
<Origin>string</Origin>
<Destination>string</Destination>
<FromDate>string</FromDate>
<ToDate>string</ToDate>
<Class>string</Class>
<Airlines>string</Airlines>
<Adults>int</Adults>
<Childs>int</Childs>
<Infants>int</Infants>
<isReturn>boolean</isReturn>
<CompanyCode>string</CompanyCode>
<sessionId>string</sessionId>
</SearchMethod>
</soap:Body>
</soap:Envelope>
OUR CODE:
$options["soap_version"]=SOAP_1_1;
$options["trace"]=true;
$options["exceptions"]=0;
$options["UserID"]="xxxxx";
$options["Password"]="xxxxx";
$client = new SoapClient("http://www.HOST.com/KH/TestKH/WebServices.asmx?WSDL",$options);
//var_dump($client->__getTypes());
$parameters= new stdClass();
$parameters->_hap="xxxxxxxxx";
$parameters->_hapType="TEST";
//$parameters->UserID="";
//$parameters->Password="";
$parameters->Origin="AUS";
$parameters->Destination="LHR";
$parameters->FromDate="01/01/2016";
$parameters->ToDate="27/02/2016";
$parameters->Class="X";
$parameters->Airlines="";
$parameters->Adults="1";
$parameters->Childs="0";
$parameters->Infants="0";
$parameters->isReturn=true;
$parameters->CompanyCode="xxxxx";
$parameters->sessionId="xxxxxx";
$result=$client->SearchMethod($parameters);
var_dump($result);
var_dump($client->__getLastRequest());
$status=$result->SearchMethodResult;
var_dump($status);
}
catch (Exception $e){
$e -> getMessage();
var_dump($e);
}
Our request process but don't know what is the wrong with these code because show server side error "Unknown error occured!"

how to call web services using soap in php

The following is a sample SOAP 1.1 request and response.:
POST /atservices/1.5/atws.asmx HTTP/1.1
Host: webservices2.autotask.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://autotask.net/ATWS/v1_5/getZoneInfo"
<?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>
<getZoneInfo xmlns="http://autotask.net/ATWS/v1_5/">
<UserName>string</UserName>
</getZoneInfo>
</soap:Body>
</soap:Envelope>
we want to call web services of autotask using soap in php.can we get example for it
how we should call soap client.
Its output should be like this :
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>
<getZoneInfoResponse xmlns="http://autotask.net/ATWS/v1_5/">
<getZoneInfoResult>
<URL>string</URL>
<ErrorCode>int</ErrorCode>
<DataBaseType>string</DataBaseType>
<CI>int</CI>
</getZoneInfoResult>
</getZoneInfoResponse>
</soap:Body>
</soap:Envelope>
Use the PHP native SoapClient along with the service WSDL, like so:
$atservices_wsdl = "https://www.autotask.net/atservices/1.5/atws.wsdl";
$atservices_client = new SoapClient($atservices_wsdl);
$zone_info = $atservices_client->getZoneInfo("SomeUserName");
print_r($zone_info); // review the returned object converted from SOAP response.
echo $zone_info->DataBaseType; // this might work if it's not behind a Response object.
At the very least, you should be aiming for something like this. More can be found here.
$soap = new SoapClient('link/to/.wsdl');
$result = $soap->__soapCall('getZoneInfo', array('UserName' => $username));
var_dump($result);

base64_encode returns blank space before string

I'm using a site called ViaPost to post letters for my business. I'm using the SoapClient. The uploaded file must be in the base64binary format. I'm using the following code however returns empty.
$pdf_data = file_get_contents('test.pdf');
$pdf_data = base64_encode($pdf_data);
print $pdf_data;
To expand on the above:
POST /viapostcustomer.asmx HTTP/1.1
Host: api.viapost.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://api.viapost.com/CreateLetter"
<?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>
<CreateLetter xmlns="http://api.viapost.com">
<loginToken>string</loginToken>
<name>string</name>
<description>string</description>
<FileContents>base64Binary</FileContents>
<dynamic>boolean</dynamic>
<shareLetterWithGroup>boolean</shareLetterWithGroup>
<returnMessage>string</returnMessage>
<letterID>long</letterID>
</CreateLetter>
</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>
<CreateLetterResponse xmlns="http://api.viapost.com">
<CreateLetterResult>boolean</CreateLetterResult>
<returnMessage>string</returnMessage>
<letterID>long</letterID>
</CreateLetterResponse>
</soap:Body>
</soap:Envelope>
<?php
$client = new SoapClient("http://api.viapost.com/viapostcustomer.asmx?WSDL");
$params = array('sUserName'=>'USERNAME','sPassword'=>'PASSWORD','sLoginToken'=>'','sReturnMessage'=>'');
$SignIn = $client->__soapCall('SignIn', array($params));
$sLoginToken = $SignIn->sLoginToken;
$pdf_data = file_get_contents('test.pdf');
$pdf_data = base64_encode($pdf_data);
$params = array('loginToken'=>$sLoginToken,'name'=>'testname','description'=>'testdescription','fileContents'=>$pdf_data,'dynamic'=>'true','shareLetterWithGroup'=>'false','returnMessage'=>'','letterID'=>'');
$CreateLetter = $client->__soapCall('CreateLetter', array($params));
$returnMessage = $CreateLetter->returnMessage;
print $returnMessage;
?>
it returns: There was a problem saving your file. Please try again.
I've done something daft I know I have.
base64_encode will return empty string if the passed argument is itself empty. You should check this before you pass data to this function.
if(strlen($pdf_data)>0){
$pdf_data = base64_encode($pdf_data);
}else{
throw new Exception("Empty File provided");
}
Try increasing error reporting, and the odd check for failure wouldn't hurt either. This should help. :)
<?php
error_reporting(-1);
ini_set('display_errors', true);
$pdf_data = file_get_contents('test.pdf');
if( ! $pdf_data) {
print 'Could not read file';
exit;
}
$pdf_data = base64_encode($pdf_data);
print $pdf_data;

how to call soap server using nusoap

i need to call a soap server, they provide request format like this (please see this page https://book.mylimobiz.com/api/ApiService.asmx?op=Test)
POST /api/ApiService.asmx HTTP/1.1
Host: book.mylimobiz.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://book.mylimobiz.com/api/Test"
<?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>
<Test xmlns="https://book.mylimobiz.com/api">
<apiId>string</apiId>
<apiKey>string</apiKey>
</Test>
</soap:Body>
</soap:Envelope>
I am using php nusoap to send request, and here is ho i am trying
require_once('lib/nusoap.php');
$serverPath = "https://book.mylimobiz.com/api/ApiService.asmx";
$param = array("apiId"=>"someapi","apiKey"=>"somekYE");
$client = new SoapClient($serverPath);
$tt = $client->call("Test",$param,"https://book.mylimobiz.com/api","https://book.mylimobiz.com/api/Test");
which is not working
can some one guide me how to request using nusoap or some thing else.
Thanks
You can do it like this:
$client = new SoapClient('https://book.mylimobiz.com/api/ApiService.asmx?WSDL');
$params = array();
$params["apiId"] = apiId;
$params["apiKey"] = apiKey;
$result = $client->Test($params);

Categories