soap client error bad request - php

I need to use a webservice with php and I got the bad request error,
this is my code:
try{
$client = new \SoapClient("http://54.88.59.192/Cashier/SwCashier.asmx?wsdl");
$params = new \SoapVar('<?xml version="1.0" encoding="UTF-8"?><XMLTransaccion><DatosTransaccion><idTransaccion>06b09c6b-f9e3-4371-a3ef-4af947d2e488</idTransaccion><idServicio>0001</idServicio><tipoTransaccion>00</tipoTransaccion><fechaTransaccion>20160527015625</fechaTransaccion><usuarioRed>pagofacil</usuarioRed><passwordRed>123456PF</passwordRed><usuarioCaja>jperez</usuarioCaja><idCaja>0016</idCaja><idRed>6548</idRed></DatosTransaccion><DatosServicio><idCliente>pepe#gmail.com</idCliente><username>pepe</username><name>pepen</name><lastname>pepe#gmail.com</lastname></DatosServicio></XMLTransaccion>', XSD_ANYXML);
$res = $client->GetServiceTransaction($params);
$res->GetServiceTransactionResponse();
var_dump($res);
}catch(\soapFault $e){
var_dump($e);
}
And the Data should be sent by xml this is the request:
<?xml version="1.0" encoding="UTF-8"?>
<XMLTransaccion>
<DatosTransaccion>
<idTransaccion>06b09c6b-f9e3-4371-a3ef-4af947d2e488</idTransaccion>
<idServicio>0001</idServicio>
<tipoTransaccion>00</tipoTransaccion>
<fechaTransaccion>20160527015625</fechaTransaccion>
<usuarioRed>pagofacil</usuarioRed>
<passwordRed>123456PF</passwordRed>
<usuarioCaja>jperez</usuarioCaja>
<idCaja>0016</idCaja>
<idRed>6548</idRed>
</DatosTransaccion>
<DatosServicio>
<idCliente>pepe#gmail.com</idCliente>
<username>pepe</username>
<name>pepen</name>
<lastname>pepe#gmail.com</lastname>
</DatosServicio>
</XMLTransaccion>

Try to check validity of your request:
$client = new \SoapClient("http://54.88.59.192/Cashier/SwCashier.asmx?wsdl", array('trace' => 1));
$params = new \SoapVar('GetServiceTransaction($params);
echo "REQUEST:\n" . $client->__getLastRequest() . "\n";

Related

php soap client issue

I have to develop a php client which connects to a soap webservice server.
With SoapUI, I can test the webservice which is working fine.
But from my php client, I get the following error:
syntax error near << from >>
Here is my php client code:
$wsdl = 'http://intrageo.cannes.fr:81/AdresseRecherche/searchByWhereClause?wsdl';
$trace = true;
$exceptions = false;
$xml_array['context'] = '?';
$xml_array['table'] = 'adr_digadr';
$xml_array['colonneARecuperer'] = 'numero';
$xml_array['clauseWere'] = 'nomvoie= \'BOULEVARD COINTET\'';
$xml_array['nbMaxLignes'] = 10;
try {
$client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
$response = $client->getDistinctValue($xml_array);
} catch (Exception $e) {
echo "Error!";
echo $e->getMessage();
echo 'Last response: '. $client->__getLastResponse();
}
var_dump($response);
Thanks for help.
It works if I use $response=$client->_soapCall("getDistinctValue",$xml_array); instead of $response=$client->getDistinctValue($xml_array)

Sample PHP code to programmatically check SOAP connection in Magento 2

I tried the below code though I am not sure whether these are the required set of scripts, but it didn't work and gives
SOAP-ERROR: Parsing WSDL: Couldn't load from : Start tag expected, '<' not found
$wsdlUrl = 'http://localhost/magento20_0407/soap/default?wsdl_list=1';
$apiUser = 'testUser';
$apiKey = 'admin123';
$token = 'xioxnuuebn7tlh8ytu7781t14w7ftwmp';
$opts = array('http' => array('method' => "GET", 'header' => "Accept-language: en\r\nConnection: close\r\n"));
$context = stream_context_create($opts);
stream_context_set_option($context, "http", "protocol_version", 1.1);
fpassthru(fopen($wsdlUrl, 'r', false, $context));
$opts = array('http'=>array('header' => 'Authorization: Bearer '.$token));
$serviceArgs = array("id"=>1);
try{
$context = stream_context_create($opts);
$soapClient = new SoapClient($wsdlUrl, array('version' => SOAP_1_2, 'context' => $context));
$soapResponse = $soapClient->customerCustomerAccountServiceV1($serviceArgs);
}catch(Exception $e){
$e->getMessage();
}
var_dump($soapResponse);exit;
Can anyone share the code to make SOAP connection in Magento2.x
In Magento1.x the below code works fine to connect SOAP
$apiUrl = 'http://localhost/magento_28_03/index.php/api/soap?wsdl';
$apiUser = 'testUser';
$apiKey = 'admin123';
ini_set("soap.wsdl_cache_enabled", "0");
try{
$client = new SoapClient($apiUrl, array('cache_wsdl' => WSDL_CACHE_NONE));
} catch (SoapFault $e) {
echo 'Error in Soap Connection : '.$e->getMessage();
}
try {
$session = $client->login($apiUser, $apiKey);
if($session) echo 'SOAP Connection Successful.';
else echo 'SOAP Connection Failed.';
} catch (SoapFault $e) {
echo 'Wrong Soap credentials : '.$e->getMessage();
}
But the above doesn't work for Magento 1. Can anyone say, what changes the above code needs to work fine for Magento 2?
For SOAP API call follow the below
test.php
<?php
$token = 'YOUR_ACCESS_TOKEN';
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$addArgs = array('num1'=>2, 'num2'=>1);// Get Request
$sumArgs = array('nums'=>array(2,1000));// Post request
//$wsdlUrl = YOUR_BASE_URL."soap?wsdl&services=customerAccountManagementV1,customerCustomerRepositoryV1,alanKentCalculatorWebServiceCalculatorV1";//To declar multiple
$wsdlUrl = YOUR_BASE_URL."soap?wsdl&services=alanKentCalculatorWebServiceCalculatorV1";
try{
$opts = ['http' => ['header' => "Authorization: Bearer " . $token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
}catch(Exception $e){
echo 'Error1 : '.$e->getMessage();
}
try{
$soapResponse = $soapClient->alanKentCalculatorWebServiceCalculatorV1Add($addArgs);print_r($soapResponse);
$soapResponse = $soapClient->alanKentCalculatorWebServiceCalculatorV1Sum($sumArgs);print_r($soapResponse);
}catch(Exception $e){
echo 'Error2 : '.$e->getMessage();
}
?>
http://YOUR_BASE_URL/test.php
SOAP-ERROR: Parsing WSDL: Couldn't load from : Start tag expected, '<' not found
Tells you all you need to know; the URL you're trying to load isn't a proper WSDL.
What are the contents of: http://localhost/magento20_0407/soap/default?wsdl_list=1

How to handle SoapClient Fault In PHP

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());
}

SOAP function giving me a 500 error

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>

How to call web service with auth using php

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) );

Categories