I am trying to access a WCF service using the PHP soap client. I keep receiving the error: "Error Fetching http headers" I tried increasing the default_socket_time as well. I need to use basic authentication as well as parameters for the methods when accessing the services. Following is my PHP code. I am unable to change the server configurations.
<?php
$wsdl = "https://example.com/NewServer/Services/WCFServices.svc?wsdl";
$url=$wsdl;
$svc = 'WCFServices';
$func = 'getValId';
$username = "admin";
$password = "admin";
$apiauth =array('UserName'=>$username,'Password'=>$password);
$authHeader = new SoapHeader('https://tempuri.org/', 'AuthHeader', $apiauth);
$client = new SoapClient($url, array('soap_version' => SOAP_1_2,
'exceptions' => true,
'trace' => true);
try
{
ini_set('default_socket_timeout', 600);
$client->__setSoapHeaders($authHeader);
$info = $client->__soapCall($func, array('extRef' => 'ABCD'));
var_dump($info);
}
catch (SoapFault $fault)
{
var_dump($fault);
$xml=$fault->faultstring;
die;
}
?>
Guidance into correct path would be appreciated.
CodePudding user response:
The WCF service which I was invoking had both HTTP and basic bindings available. So I invoked the basic endpoint which uses SOAP 1.1 version. Posting the code as it will be useful to someone in the future.
<?php
class WsseAuthHeader extends SoapHeader
{
private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
function __construct($user, $pass, $ns = null)
{
if ($ns)
{
$this->wss_ns = $ns;
}
$auth = new stdClass();
$auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
$auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
$username_token = new stdClass();
$username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);
$security_sv = new SoapVar(
new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns),
SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);
parent::__construct($this->wss_ns, 'Security', $security_sv, true);
}
}
$options = array(
'soap_version' => SOAP_1_1,
'exceptions' => true,
'trace' => 1,
'wdsl_local_copy' => true);
$username = "username";
$password = "password";
$wsse_header = new WsseAuthHeader($username, $password);
$client = new SoapClient('https://ServerName/WCFService.svc?wsdl', $options);
$client->__setSoapHeaders(array($wsse_header));
$client->__setLocation('https://ServerName/WCFService.svc/basicSecurity');
try
{
$params = array('param1' => 'xxxx');
$phpresponse = $client->__soapCall('methodname', array('parameters' => $params));
var_dump($phpresponse);
echo "</b><BR/><BR/>";
}
catch(Exception $e)
{
echo "<h2>Exception Error!</h2></b>";
echo $e->getMessage();
}
?>