However, what I want is to be able to get response request and populate PHP
I already did that using soapUI and got the response in xml format like
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://bsestarmfdemo.bseindia.com/2016/01/IMFUploadService/MFAPIResponse</a:Action>
<a:RelatesTo>uuid:804ed18e-630f-4b16-86ee-6f97ee71743e</a:RelatesTo>
</s:Header>
<s:Body>
<MFAPIResponse xmlns="http://bsestarmfdemo.bseindia.com/2016/01/">
<MFAPIResult>100|PAYMENT NOT INITIATED FOR GIVEN ORDER</MFAPIResult>
</MFAPIResponse>
</s:Body>
</s:Envelope>
MY PHP Code
I don’t have much experience with SOAP but am required to build a validator for my work and using a specific SOAP wsdl.
I’ve set up the connect
<?php
$url= "http://bsestarmfdemo.bseindia.com/StarMFFileUploadService/StarMFFileUploadService.svc?wsdl";
$method = "MFAPI";
$error=0;
$fault=0;
$client = new SoapClient($url, array('soap_version' => SOAP_1_2 , 'SoapAction'=>'http://tempuri.org/IStarMFFileUploadService/MFAPI'));
$actionHeader= array();
$actionHeader[] = new SoapHeader('http://www.w3.org/2005/08/addressing',
'Action',
'http://tempuri.org/IStarMFFileUploadService/MFAPI');
$actionHeader[] = new SoapHeader('http://www.w3.org/2005/08/addressing',
'To',
'http://bsestarmfdemo.bseindia.com/MFUploadService/MFUploadService.svc/Basic');
$client->__setSoapHeaders($actionHeader);
$featchData = array('Param' => array('UserId' => 'xxxxx', 'Flag' => 'xxxx', 'EncryptedPassword' => 'xxxx', 'param' => 'xxxx') );
try{
$resultData = $client->__call($method, array($featchData));
}
catch (SoapFault $resultData) {
$error = 1;
}
if($error==1) {
$result=$fault;
}else{
$result = $resultData;
var_dump($result);
}
// echo $result;
?>
Related
Im trying to connect to a web service from php (WorkPress)whit soapClient and i'm getting this: 'The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.'
Any idea?
This is what I´m doing, the responce is a true or false.
$wsdlUrl = 'https://#####.svc?wsdl';
$client = new SoapClient($wsdlUrl);
var_dump($client->__getTypes());
echo '</br>';
$xmlr = new SimpleXMLElement('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:ExisteMiembro>
<!--Optional:-->
<tem:carnet>######</tem:carnet>
<!--Optional:-->
<tem:key>#######</tem:key>
</tem:ExisteMiembro>
</soapenv:Body>
</soapenv:Envelope>');
$params = new stdClass();
$params->xml = $xmlr->asXML();
$result = new SimpleXMLElement($client->ExisteMiembro($params));
Solved, I used this code instead and it works,
ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 900);
ini_set('default_socket_timeout', 15);
try {
$opts = array(
'http' => array(
'user_agent' => 'PHPSoapClient'
)
);
$context = stream_context_create($opts);
$wsdlUrl = 'https://######.svc?wsdl';
$soapClientOptions = array(
'stream_context' => $context,
'cache_wsdl' => WSDL_CACHE_NONE
);
$client = new SoapClient($wsdlUrl, $soapClientOptions);
$checkVatParameters = array(
'carnet' => '###',
'key' => '###'
);
$result = $client->ExisteMiembro($checkVatParameters);
print_r($result);
return true;
}
catch(Exception $e) {
echo $e->getMessage();
var_dump("entro al msj de error");
}
I am trying to create SOAP request, but it gives me error: An exception occurred whilst trying to authenticate you. String reference not set to an instance of a String. Parameter name: s
My code is:
$wsdl = "https://securedwebapp.com/api/service.asmx?WSDL";
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:kas="KashFlow">
<soapenv:Header/>
<soapenv:Body>
<kas:GetInvoice>
<kas:UserName>xxx</kas:UserName>
<kas:Password>xxx</kas:Password>
<kas:InvoiceNumber>184576</kas:InvoiceNumber>
</kas:GetInvoice>
</soapenv:Body>
</soapenv:Envelope>';
//var_dump($xml);
$args = array(new SoapVar($xml, XSD_ANYXML));
$res = $client->__soapCall('GetInvoice', $args);
var_dump($res);
echo "<hr>Last Request";
echo "<pre>", htmlspecialchars($client->__getLastRequest()), "</pre>";
Does anyone know how to solve? The request works fine if I use:
$wsdl = 'https://securedwebapp.com/api/service.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$params = array(
'InvoiceNumber' =>'81217',
'UserName' => 'xxx',
'Password' => 'xxx'
);
$resp = $client->GetInvoice($params);
print_r($resp);
This is request with simple parameters, but in real situation I have complex XML request, that I do not know how to form as php object. That is why I would send it as XML as I am trying to do at first place.
Nowadays, it is strongly advised to use a WSDL to PHP generator in order to avoid encountering this sort of issues.
I recomend using the PackageGenerator project which allows to generate a PHP SDK based on the WSDL. The generated PHP SDK contains all that is necessary to construct the request, send the request and handle the response only using an OOP approach.
I am calling a third party web service to get the user detail by the supplied credential. Below is my Soap Client Request.
$client = new \SoapClient($url, array("exception" => 0));
$auth = '
<wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>' . $userName . '</wsse:Username>
<wsse:Password>' . $password . '</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>';
$authvalues = new \SoapVar($auth, XSD_ANYXML);
$header1 = new \SoapHeader("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", $authvalues, true);
$header2 = new \SoapHeader("http://xmlns.myweb.com/FulfillProductRequest/V1", "v1");
$header3 = new \SoapHeader("http://xmlns.myweb.com/ParameterType/V2", "v2");
$header4 = new \SoapHeader("http://xmlns.myweb.com/RequestHeader/V3", "v3");
$header = array();
$header[] = $header1;
$header[] = $header2;
$header[] = $header3;
$header[] = $header4;
$client->__setSoapHeaders($header);
$res = $client->FulfillProduct($FulfillProductRequest);
When i am calling the WSDL,if i am getting the success response then there is no issue.
But if there is any error come in soapFault then my code shows fatal error.
Anybody have any idea how to handle the SoapFault plz share.
Thanks in anvance.
You can try to wrap your $res = $client→FulfillProduct($FulfillProductRequest); in a try { } catch() {}.
For example:
try {
$res = $client→FulfillProduct($FulfillProductRequest);
} catch(Exception $e) {
// An error has occured.
// All information about the error has been stored in variable $e
print_r($e);
}
Be aware, that you can set the SoapClient to never throw exceptions with your settings, so look into that first.
Maybe it will help someone.
$options = ['trace' => true,
'debug' => false,
"exceptions" => true,
'version' => SOAP_1_2,
'connection_timeout' => 15];
$wsdl = "http://test.com?wsdl";
try {
$soapClient=new SoapClient($wsdl,$options);
} catch (SoapFault $e) {
logError($e->getMessage(),$e->getCode());
}
I'm trying to connect to a soap api, but every time i try and call a function i get a 500 internal error.
I able able to call $client->__getFunctions() with out any issue, so i'm guessing that the code was able to connect to the soap server ok? I can see the function that i'm trying to call in the array:
GetCatalogFullResponse GetCatalogFull(GetCatalogFull $parameters)
heres my code:
$GUID = '000-000-000-000';
$user = 'user';
$pass = 'pass';
$url = 'https://example.net/webservices.asmx?WSDL';
$localCert = "cert.pem";
$options = array(
'local_cert' => $localCert,
'user' => $user,
'passphrase' =>$pass,
'soap_version' => SOAP_1_2,
);
try {
$client = new SoapClient($url, $options);
$result = $client->GetCatalogFull(['AccountGUID'=>$GUID,'UserLogin'=>$user]);
echo '<pre>';
print_r($client->__getFunctions());
} catch (SoapFault $fault) {
trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
this is the request i am trying to make with the soap client class:
<?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>
<GetCatalogFull xmlns="http://www.example.com">
<AccountGUID>string</AccountGUID>
<UserLogin>string</UserLogin>
</GetCatalogFull>
</soap12:Body>
</soap12:Envelope>
I need to make a call to the webservice bellow, but so far failed to create the code for it.
https://www.nightline-delivers.com/ParcelMotel_UAT/RetailService.svc?wsdl
The request needs to look like the following:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ret="Retail">
<soapenv:Header>
<UserName>WebServiceSample</UserName>
<Password>password</Password>
</soapenv:Header>
<soapenv:Body>
<ret:ValidateCredentials>
<ret:motelId>BAC032</ret:motelId>
<ret:mobileNumber>0865596800</ret:mobileNumber>
</ret:ValidateCredentials>
</soapenv:Body>
</soapenv:Envelope>
My code so far looks like this:
$client = new SoapClient("https://www.nightline-delivers.com/ParcelMotel_UAT/RetailService.svc?wsdl");
$location = new StdClass();
$location->motelId = 'BAC032';
$location->mobileNumber = '0866696800';
$auth = array(
'UserName'=>'WebServiceSample',
'Password'=>'password',
);
$header = new SoapHeader('NAMESPACE','Auth',$auth,false);
$client->__setSoapHeaders( $header );
try {
$response = $client->ValidateLocation( $location );
var_dump($response);
} catch( Exception $e ) {
echo $e->getMessage();
}
According to this it is impossible in PHP to get a header element without namespace (which is the case in your example) so that is not possible with SoapClient. However you may try to send request where both UserName and Password fields are in the Retail namespace, maybe server will accept this:
<?php
$client = new SoapClient("https://www.nightline-delivers.com/ParcelMotel_UAT/RetailService.svc?wsdl", array('trace' => TRUE));
$location = new StdClass();
$location->motelId = 'BAC032';
$location->mobileNumber = '0866696800';
$auth = array(
'UserName'=>'WebServiceSample',
'Password'=>'password',
);
$headers = array();
foreach($auth as $k=>$v) {
$headers[] = new SoapHeader('Retail', $k, $v);
}
$client->__setSoapHeaders( $headers );
try {
$response = $client->ValidateCredentials( $location );
var_dump($response);
} catch( Exception $e ) {
echo "REQUEST: ".$client->__getLastRequest();
echo $e->getMessage();
}
This code also will show you request if an exception occurs, and I have changed ValidateLocation to ValidateCredentials as in your example message. It produces following message:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="Retail">
<SOAP-ENV:Header>
<ns1:UserName>WebServiceSample</ns1:UserName>
<ns1:Password>password</ns1:Password>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:ValidateLocation>
<ns1:motelId>BAC032</ns1:motelId>
<ns1:mobileNumber>0866696800</ns1:mobileNumber>
</ns1:ValidateLocation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I got it working. Here is the code:
<?php
$username = 'WebServiceSample';
$password = 'password';
function formatXML($xml)
{
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->loadXML($xml);
return $doc->saveXML();
}
try {
$client = new SoapClient("https://www.nightline-delivers.com/ParcelMotel_UAT/RetailService.svc?wsdl", array(
'trace' => true,
));
$headers = array();
$headers[] = new SoapHeader('Retail', 'UserName', 'WebServiceSample');
$headers[] = new SoapHeader('Retail', 'Password', 'password');
$client->__setSoapHeaders($headers);
$params = array(
'motelId' => 'BAz031',
'mobileNumber' => '0866696800'
);
$result = $client->__soapCall('ValidateLocation', array('ValidateLocation' => $params), array(
'location' => 'https://www.nightline-delivers.com/ParcelMotel_UAT/RetailService.svc/basic?APIKey=7C3A0EA9-C8D6-4DB0-ADF0-615BFD29DC1D'
));
} catch (SoapFault $e) {
exit($e->getMessage());
}
$request = $client->__getLastRequest();
$response = $client->__getLastResponse();
echo '<pre>';
echo htmlspecialchars( formatXML($request) );
echo "\n";
echo htmlspecialchars( formatXML($response) );