Savon header authentication problems - php

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

Related

set soapclient HTTP_METHOD

Im trying to consume a webserivce usnig soapclient.
The instructions for the connection is to use:
request method = "POST";
request ContentType = "application/soap+xml";
This is the code im using to create the client
public function init_client(){
if( $this->env == 'dev'){
$api_url = self::$test_url;
}else{
$api_url = self::$production_url;
}
try{
$options = array(
'soap_version' => SOAP_1_2
);
$this->client = new SoapClient( $api_url , $options );
return true;
}catch(Exception $e){
return $e->getMessage();
}
}
How can i set the client's method and content type ?

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!

Soap body-signing to Envelope header

I have problem with Osuuspankki Webservice. I managed to get Certificate but when i need to get data from Bank there is few problems.
function getArgumentsToDownload($customerid=null,$private_key=null, $public_key=null,$status="NEW",$action=null,$start_date=null,$end_date=null) {
$xml = $this->createDocument();
$ApplicactionRequest = $xml->createElement("ApplicationRequest");
$ApplicactionRequest->setAttribute("xmlns", "http://bxd.fi/xmldata/");
$CustomerId = $xml->createElement("CustomerId",$customerid);
$ApplicactionRequest->appendChild($CustomerId);
$Timestamp = $xml->createElement("Timestamp",date("c"));
$ApplicactionRequest->appendChild($Timestamp);
$Status = $xml->createElement("Status",$status);
$ApplicactionRequest->appendChild($Status);
$Environment = $xml->createElement("Environment",$this->Environment);
$ApplicactionRequest->appendChild($Environment);
$SoftwareId = $xml->createElement("SoftwareId",$this->SoftwareId);
$ApplicactionRequest->appendChild($SoftwareId);
$xml->appendChild($ApplicactionRequest);
$objDSig = new XMLSecurityDSig();
$objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N_COMMENTS);
$objDSig->addReference($xml, XMLSecurityDSig::SHA1, array('http://www.w3.org/2000/09/xmldsig#enveloped-signature'));
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type'=>'private'));
//$objKey->loadKey($private_key,true);
$objKey->loadKey($private_key,true);
$objDSig->sign($objKey);
$objDSig->add509Cert(file_get_contents($public_key),false);
$objDSig->appendSignature($xml->documentElement);
$signed_XML = $xml->saveXML();}
`
I Can sign Env body but it needed to sign again to Env-header.
https://www.op.fi/media/liitteet?cid=151240134&srcpl=4 (page 22/44).
public function getFileList($username=null, $private_key=null, $public_key=null,$status='NEW',$start_date=null, $end_date=null) {
// Alustetaan muuttujat joita käytetään SOAP Clientissä datan lataamisessa Webservicestä.
$arguments = $this->CI->getArgumentsToDownload($username,$private_key,$public_key);
$client = new SoapClient($this->wsdl_test, array('trace' => true, 'exceptions' => false));
// Asetetaan salainen avain
$client->setPrivateKey($private_key);
// Asetetaan julkinen avain
$client->setPublicKey($public_key);
$soap_header = new SoapHeader($this->ws_ns, "Timestamp",array("expires"));
$client->__setSoapHeaders($soap_header);
$return = $client->__soapCall('downloadFileList', array($arguments));
}
What should i do that i get signed Envelope?

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

Categories