How to construct SOAP header using SoapClient in PHP - php

I need to make soap calls which include headers like this:
<soap:Header>
<Agency xmlns="http://schemas.costacrociere.com/WebAffiliation">
<Code>1111</Code>
<Culture />
</Agency>
<Partner xmlns="http://schemas.costacrociere.com/WebAffiliation">
<Name>AAAA</Name>
<Password>XXXX</Password>
</Partner>
</soap:Header>
How to do that in PHP using SoapClient?
Plz help :)

I did it:
$ns="http://schemas.costacrociere.com/WebAffiliation";
//Body of the Soap Header.
$headerbody1 = array("Name" => "AAAA", "Password" => "XXXX");
$headerbody2 = array("Code" => "1111");
//Create Soap Header.
$header[] = new SOAPHeader($ns, 'Partner', $headerbody1);
$header[] = new SOAPHeader($ns, 'Agency', $headerbody2);
//set the Headers of Soap Client.
$client->__setSoapHeaders($header);

Related

SoapClient PHP code without WSDL from example CURL request

I am trying to call a SOAP API using PHP and have been given the following example CURL call:
curl --location --request POST 'https://soapurl.test/mysoapcall'
--header 'Content-Type: application/xml'
--header 'client-id: <<your client id>>'
--header 'client-secret: <<your client secret>>'
--header 'Authorization: Bearer <<token>>'
--data-raw '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header></env:Header>
<S:Body>
<ns0:getReturnedValues xmlns:ns0="https://soapurl.test/anotherurl1" xmlns:ns1="https://soapurl.test/anotherurl2">
<ns0:var1>100</ns0:var1>
<ns0:var2>XXX</ns0:var2>
</ns0:getReturnedValues>
</S:Body>
</S:Envelope>'
I can successfully call the above via Postman, but when I am writing the PHP code using SoapClient, I keep getting "One or more required API parameters are missing in the API request."
I know that I must have something wrong with how I am defining the SOAP headers or how I am defining the uri, but I can't work out what it is.
Here is my PHP code:
$client = new SoapClient(null, array('location' => "https://soapurl.test/mysoapcall",
'uri' => "https://soapurl.test/anotherurl1");
$ns = 'https://soapurl.test/anotherurl1';
$headers = array();
$headers[] = new SoapHeader($ns,
'client-id',
'my_client_id');
$headers[] = new SoapHeader($ns,
'client-secret',
'my_client_secret');
$headers[] = new SoapHeader($ns,
'Authorization',
'Bearer my_bearer_token');
$client->__setSoapHeaders($headers);
$client->__soapCall("getReturnedValues", array('var1'=>'100', 'var2' => 'XXX'));
$client->__soapCall("getReturnedValues", [
new \SoapParam('100', 'var1'),
new \SoapParam('XXX', 'var2')
]);
My PHP code generates the following request:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:enc="http://www.w3.org/2003/05/soap-encoding" xmlns:ns1="https://soapurl.test/anotherurl1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<ns1:getReturnedValues env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<var1 xsi:type="xsd:string">100</var1>
<var2 xsi:type="xsd:string">XXX</var2>
</ns1:getReturnedValues>
</env:Body>
</env:Envelope>
Which is obviously not the same as the example I was supplied with.
I'm new to SOAP, so please can someone advise on what I am doing wrong? Many thanks

PHP: How to do a SOAP request from this XML?

Can someone help me?
I can not perform the AdvogadoRegular function in this web service.
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:int="https://www5.oab.org.br/integracao/">
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<token xmlns="http://CFOAB.Integracao">xxxxxxxx</token>
<wsa:To>https://www5.oab.org.br/Integracao/CNA.svc</wsa:To>
</soap:Header>
<soap:Body>
<int:AdvogadoRegular>
<!--Optional:-->
<int:cpf>99999999999</int:cpf>
</int:AdvogadoRegular>
</soap:Body>
</soap:Envelope>
I try to access from that code.
// WSDL
$client = new SoapClient('https://www5.oab.org.br/Integracao/CNA.svc?wsdl', $options = array(
'soap_version' => SOAP_1_2,
'trace'=>1,
'exceptions'=> 0
));
// NOME DA FUNÇÃO A SER EXECUTADA
$function = 'AdvogadoRegular';
// PARÂMETROS DA FUNÇÃO A SER EXECUTADA
$arguments= array('AdvogadoRegular' => array( 'cpf' => '99999999999' ));
// URL DO WEB SERVICE
$options = array('location' => 'https://www5.oab.org.br/Integracao/CNA.svc');
// HEADER
$token = array(
'token'=> 'xxxxxxxx',
'wsa:To'=>'https://www5.oab.org.br/Integracao/CNA.svc',
);
$header = new SoapHeader("http://CFOAB.Integracao",'token',$token,false);
$client->__setSoapHeaders($header);
// EXECUTA A FUNÇÃO
$result = $client->__soapCall($function, $arguments, $options);
echo $result;
I think the error happens when I try to send the header. Please help me...
first I would advise you to use a WSDL to php generator such as PackageGenerator, then regarding your header, it seems it's a WsSecurity header that you have to send, so you should try using the WsSecurity project that eases the way to add this type of header

How to set this soap header on a SOAP CALL PHP?

<SOAP-ENV:Header>
<Seguridad>
<usuario>0000000000</usuario>
<password>9FDBDE265822755C50dHD5D33B61580736ECB94978BC40DDD2D4220CB63FE7E</password>
<fechaSistema>02/01/2015</fechaSistema>
</Seguridad>
</SOAP-ENV:Header>
I need to add this header to the soap call, but, the examples that I found need some namespace, and result so different of this.
I used NuSoap.php
$wsdl = "https://www.convolmiscelaneapruebas.pemex.com/ServiciosCVWEB/ServicioEnviaCONVOLService/ServicioEnviaCONVOLService.wsdl";
$client = new nusoap_client($wsdl,TRUE);
$header =
"<Seguridad>
<usuario>0001250000</usuario>
<password>4e671bf08913dn8k09d6359262117c8e67a5507b165f727288a487040bf2a1780</password>
<fechaSistema>02/01/2015</fechaSistema>
</Seguridad>";
$operation = array('arg0' => '2012-08-25',
'arg1' => '11:48:46',
'arg2'=>'12095866a6l9b7dbcd44640189c57hlZ5918192739040eb52ba5b==',
'arg3'=>'ZmU5866axMzc3ZDQyJmdAyZjM2YTc57Db7dbcd446401YTM1Mg3Ng=='
);
$client->setHeaders($header);
$res = $client->call('enviaCONVOL',$operation);

PHP sending SOAP request with SOAP client

It's my first time with SOAP, I already read all manuals and still stacked with the following example:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<AuthenticationInfo xmlns="https://blabla.com/ws/Quoting">
<strUserName>[username]</strUserName>
<strPassword>[password]</strPassword>
</AuthenticationInfo>
</soap:Header>
<soap:Body>
<GetQuotes xmlns="https://blabla.com/ws/Quoting">
<CallerClientID>0</CallerClientID>
<quoteRequest>
<CapacityCode>2</CapacityCode>
<BabiesCount>0</BabiesCount>
<QuotingOptions>0</QuotingOptions>
</quoteRequest>
<clientIDs>
<int>20295</int>
</clientIDs>
<withUrlParam>false</withUrlParam>
</GetQuotes>
</soap:Body>
</soap:Envelope>
I do not understand how to start the soapclient with authorization, please help.
You need to use SoapHeader. Below is the example.
$ns = 'https://blabla.com/ws/Quoting'; //Namespace of the WS.
//Body of the Soap Header.
$headerbody = array('strUserName' => $someUser,
'strPassword' => $somePass);
//Create Soap Header.
$header = new SOAPHeader($ns, 'AuthenticationInfo', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);

SoapHeader formatting using PHP

I am using the following code to create a SOAPHEADER using PHP
// Instantiate the client.
$client = new SoapClient($wsdl_url, array('trace' => 1));
// Pass along login information
$soap_header = new SoapHeader(
$api_url,
'APICredentials',
array(
'DeveloperKey' => $developerKey,
'Password' => $password
)
);
$client->__setSoapHeaders($soap_header);
which created this
<SOAP-ENV:Header>
<ns2:APICredentials>
<item>
<key>DeveloperKey</key>
<value>********</value>
</item>
<item>
<key>Password</key>
<value>********</value>
</item>
</ns2:APICredentials>
</SOAP-ENV:Header>
How do I change the PHP code to create the following headers?
<soapenv:Header>
<web:APICredentials>
<web:DeveloperKey>...</web:DeveloperKey>
<web:Password>...</web:Password>
</web:APICredentials>
</soapenv:Header>
You can set this completely manual by using $soap_client->__setSoapHeaders($header);
Required SOAP Header:
<soap:Header>
<RequestorCredentials xmlns="http://namespace.example.com/">
<Token>string</Token>
<Version>string</Version>
<MerchantID>string</MerchantID>
<UserCredentials>
<UserID>string</UserID>
<Password>string</Password>
</UserCredentials>
</RequestorCredentials>
</soap:Header>
Corresponding PHP code:
<?php
$ns = 'http://namespace.example.com/'; //Namespace of the WS.
//Body of the Soap Header.
$headerbody = array('Token' => $someToken,
'Version' => $someVersion,
'MerchantID'=>$someMerchantId,
'UserCredentials'=>array('UserID'=>$UserID,
'Password'=>$Pwd));
//Create Soap Header.
$header = new SOAPHeader($ns, 'RequestorCredentials', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);
Source: http://php.net/manual/en/soapclient.setsoapheaders.php

Categories