PHP Parse SOAP envelope to XML - php

I been at this all day and feel like I'm getting nowhere, I need to get this to display as an xml like how it is on this page http://admin.stock.imdfulfilment.com/api/service.asmx/GetCouriers. Yet the following code
<?
try
{
$soap_url = 'http://admin.stock.imdfulfilment.com/api/service.asmx?wsdl';
$client = new SoapClient($soap_url, array(
'trace' => 1,
'exceptions'=>true,
'GetCouriersResult' => 'xml')
);
$client->GetCouriers();
$xml = simplexml_load_string($client->__getLastResponse());
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->registerXPathNamespace('xsd', 'http://www.w3.org/2001/XMLSchema');
foreach($xml->xpath('//soap:Envelope') as $item) {
echo "$item\n\n";
}
}
catch (Exception $e)
{
print_r($e);
}
?>

It returns doubly encoded XML, I have no idea why someone thought that a Good Idea (perhaps someone who doesn't want to bother with updating their wsdl):
$soap_url = 'http://admin.stock.imdfulfilment.com/api/service.asmx?wsdl';
$client = new SoapClient($soap_url, array(
'trace' => 1,
'exceptions'=>true,
'GetCouriersResult' => 'xml')
);
$result = $client->GetCouriers();
echo $result->GetCouriersResult->any;
//or you can load it in DOM or simpleXML if you need to read it;
$the_response_should_have_been = new simpleXMLElement($result->GetCouriersResult->any);

Related

SoapFault: looks like we got no XML document Magento and laravel

i am with a problem in my application Laravel, when i use the SoapClient, for example my_object_soap->login(); returned this error:
Fatal error: Uncaught SoapFault exception: [Client] looks like we got no XML
I already tried several solutions that I researched in google and here, but I did not solve my problem.
my code follows:
ini_set("soap.wsdl_cache_enabled",0);
ini_set("soap.wsdl_cache",0);
ini_set("error_reporting",-1);
ini_set("display_errors","On");
$wsdl_url = "https://example.com/index.php/api/v2_soap/index/?wsdl";
$apiAuth = new \stdClass();
$apiAuth->username = trim("myusermagento");
$apiAuth->apiKey = trim("mykeymagento");
try{
$proxy = new SoapClient($wsdl_url,array('cache_wsdl' => WSDL_CACHE_NONE, 'trace' => true));
$session = $proxy->login($apiAuth);
$data = $session;
$status = true;
$responseStatus = 200;
} catch(SoapFault $e) {
$error = $e->getMessage();
$data = $proxy->__getLastResponse();
$status = false;
$responseStatus = 500;
}
return Response::json([
'success' => $status,
'data' => $data,
'erros'=> $error,
],$responseStatus);
I have no idea which a problem, when i tested in SoapUI, with this user and key, it's alright, but in my app not.
My magento version app is 1.6.2, and my php is 7.2, I already tried to downgrade to php 5.6 because it could be incompatibility with the magento version and my php but it still did not work.
Can someone help me?
Try this one, structure your request as follows
$options = array('trace'=> true,'exceptions' => true);
$client = new \SoapClient('https://www.example.com?wsdl',$options);
$params = new \stdClass();
$params->key1 = 'XXXXXX';
$params->Key2 = 'XXXXX';
$soapVar = new \SoapVar($params,SOAP_ENC_OBJECT);
$header = new \SoapHeader('https://www.example?wsdl','credentials',$soapVar);
$client->__setSoapHeaders(array($header));
$result=$client->login(function parameters here);
Cheers!!!

How to receive Soap response in php

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

How to create SOAP request from XML with PHP

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.

SOAP api not accepting keys

I'm trying to get data out of a SOAP API (complexType) but it's saying that my provided data is incorrect.
Errormessage I'm getting: "Wrong combination LINKCODE/LINKHASH"
(LINKCODE represents CompanyId & LINKHASH represents linkHash)
The weird thing is though, when I'm trying to get data using http://www.soapclient.com/soaptest.html, it works...
Are there things I have to provide such as schemes or encodings?
Anyway, this is my code:
<?php
include_once 'getArticles.class.php';
$objGetArticles = new getArticles();
$objGetArticles->CompanyId = 'xxx';
$objGetArticles->linkHash = 'xxx';
$objGetArticles->SyncOnlyGlobalPrices = 1;
$wsdl = "https://wcupwebservice.syscom.be/wsWCupDemo/wsWCupDemo.exe/wsdl/IwsWCup";
$client = new SoapClient($wsdl,
array (
'trace' => 1,
'exceptions' => 0,
'cache_wsdl' => WSDL_CACHE_NONE,
'keep_alive' => false
)
);
try {
$result = $client->getArticles(new SoapVar($objGetArticles, SOAP_ENC_OBJECT));
echo "<pre>";
print_r($result);
}catch (SOAPFault $f) {
echo $f->getMessage();
}catch (Exception $e) {
echo $e->getMessage();
}
getArticles.class.php:
<?php
class getArticles{
var $CompanyId;
var $linkHash;
var $SyncOnlyGlobalPrices;
}
Anybody know what to do here? I've spent so much time on this already.. Thanks!

Savon header authentication problems

I am trying to authenticate a site. I found an example for PHP, but I'm having some trouble rewriting it to Ruby.
Example code:
class HRDConfig {
public $uid = "myuid";
public $pass = "mypassword123";
const NS = "https://www.tested.site.com/partnerAPI/";
const PARTNER = "https://www.tested.site.com/partnerAPI/Partner.php?wsdl";
}
ini_set("soap.wsdl_cache_enabled", "1");
$soap = new SoapClient(HRDConfig::PARTNER, array("encoding"=>"UTF-8", "exceptions" => true));
$soap->__setSoapHeaders(array(new SoapHeader(HRDConfig::NS, "AuthHeader", new HRDConfig())));
My code:
client = Savon.client(
wsdl: 'https://www.tested.site.com/partnerAPI/Partner.php?wsdl',
soap_header:{
'AuthHeader' => {'uid'=>'myuid','pass'=>'mypass'}
})
What I'm doing wrong?
$client = new SoapClient(
'http://service.example.com?wsdl',
array('soap_version' => '1.2',
'connect_timeout' => 10,
'compression' => (SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP),
'cache_wdsl' => WSDL_CACHE_NONE));
$header = new SoapHeader(
'http://service.example.com?wsdl',
'AuthenticateRequest',
new SoapVar(array('apiKey' => '***api******'), SOAP_ENC_OBJECT),
true);
$client->__setSoapHeaders($header);
try {
echo("\n[SOAP response]\n");
$response = $client->__soapCall('your function', array());
echo("\n[After response]\n");
echo("\n{$response }\n");
}
catch (Exception $e) {
echo($e->getMessage());
}
Note: here "apiKey" is my params to authenticate api, your can be different

Categories