I'm trying to connect to a WCF service from PHP using the SoapClient class. I can successfully connect and traverse the API using WCFStorm however cannot seem to construct the header data correctly in order to authenticate from SoapClient. A working WCF request would be as follows:
<MyMethod>
<OutgoingHeaders attr0="MsgHeaderArray" isNull="false">
<MsgHeaderArray0>
<Name>ApiKey</Name>
<Namespace>http://www.service.com/namespace</Namespace>
<ItemType>System.String</ItemType>
<Value>ABC123</Value>
<Direction>Outgoing</Direction>
</MsgHeaderArray0>
</OutgoingHeaders>
<MethodParameters>
<colour>Red</colour>
<size>Large</size>
</MethodParameters>
</MyMethod>
The code I'm using to connect to the api and call the method is:
$params = array(
'colour' => 'Red',
'size' => 'Large'
);
$service = new SoapClient('http://www.service.com/service.svc?wsdl');
$header = new SoapHeader('http://www.service.com/namespace', 'ApiKey', 'ABC123', FALSE);
$service->__setSoapHeaders(array($header));
$service->MyMethod($params);
However I'm getting an access denied error, I'm guessing because SoapHeader isn't corrently formatted?
SoapFault: Access is denied. in SoapClient->__call()
Thank you for any help you can provide.
Related
$data2=[
"user_name"=>"****",
"password"=>"****",
"customer_num"=>"***",
"process_num"=>000,
];
$wsdl = "https://vbtestservice.vakifbank.com.tr/HesapHareketleri.OnlineEkstre/SOnlineEkstreServis.svc?singleWsdl";
$client =new SoapClient($wsdl,array("trace"=>true,"exceptions"=>0));
$a=$client->__soapCall('GetirDekont', $data2);
echo $client->__getLastResponse();
Result of php : looks like we got no xml document
or response
The requested URL was rejected. Please consult with your administrator."
But
As shown in the pictures the wdsl is working at he SoapUI but how to implement the options in the pure php or in the laravel framework. Thank you you for helping !!
SoapUI return response this xml:
xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">
Peak.Integration.ExternalInbound.Ekstre/ISOnlineEkstreServis/GetirDekontResponse
</a:Action>
</s:Header>
<s:Body>
<GetirDekontResponse xmlns="Peak.Integration.ExternalInbound.Ekstre">
<GetirDekontResult
xmlns:b="http://schemas.datacontract.org/2004/07/Peak.Integration.ExternalInbound.Ekstre.DataTransferObjects"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:IslemKodu>VBD0004</b:IslemKodu>
<b:IslemAciklamasi>Talimat için dekont bulunamadı.</b:IslemAciklamasi>
<b:DekontListesi xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
</GetirDekontResult>
</GetirDekontResponse>
</s:Body>
</s:Envelope> ```
I found the solution :)
Quote from Alper Yaman, a member of Laravel Turkey group on Facebook.
To set WSA Addressing and add default wsa:To options on SoapClient connection in Laravel, you can follow these steps:
You can specify "soap_version" and "features" in the options array when creating the SoapClient object. Example:
$options = array(
'soap_version' => SOAP_1_2,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS
);
$client = new SoapClient($wsdl, $options);
You can set WSA Addressing and add default wsa:To options by editing the headers of the request sent to the SoapClient object. Example:
$headers = array(
new SoapHeader("http://www.w3.org/2005/08/addressing", "Peak.Integration.ExternalInbound.Ekstre/ISOnlineEkstreServis/GetirDekont", $to),
new SoapHeader("http://www.w3.org/2005/08/addressing", "WEB SERVICE LINK ENDING WITH ?WSDL HERE", $action)
);
$client->__setSoapHeaders($headers);
Note: The $to and $action variables are required values for WSA Addressing.
After doing these operations, try to connect to the web service with the SoapClient object.
If you still get the error, please contact the server's administrator to ensure that the URL is not rejected.
I am trying to connect to a SOAP webservice using php. I am very new to using php.
I can connect to the service, the test below returns a list of all the available functions of the webservice.
$url = "http://...client_ip.../dkServiceDefault/dkWSItemsCGI.exe/wsdl/IItemService";
$client = new SoapClient($url);
var_dump($client->__getFunctions());
If I try to access one of these functions(ex. NumberOfModifiedItems) then I get an error stating that I need to supply a SOAP header with a username and password.
According to the documentation of the SOAP service the header needs to look like this:
<soap:Header>
<q1:BasicSecurity id="h_id1" xmlns:q1="urn:dkWSValueObjects">
<Username xsi:type="xsd:string">username</Username>
<Password xsi:type="xsd:string">password</Password>
</q1:BasicSecurity>
</soap:Header>
How can I make this header in php? How do I attach it to the SoapClient? I have a username and password but I can't figure out how to create the exact header to send to the webservice. I have tried following several tutorials, but I just can't seem to get it to work.
You may pass SOAP headers with SoapHeader class and SoapClient::__setSoapHeaders method:
<?php
$url = "http://...client_ip.../dkServiceDefault/dkWSItemsCGI.exe/wsdl/IItemService";
$client = new SoapClient($url);
$namespace = "urn:dkWSValueObjects";
$authentication = array(
'Username' => 'yourname',
'Password' => 'yourpassword'
);
$header = new SoapHeader($namespace, 'BasicSecurity', $authentication, false);
$client->__setSoapHeaders($header);
var_dump($client->__getFunctions());
?>
I am newbie to services, i am currently calling .net WCF service in PHP using SoapClient object. Code is as follows:
$arr2=array('paymentVendorCode'=>"test1",'transactionNumber'=>123456789,'dataSource'=>'Ghana_QA','customerNumber'=>45678912,'amount'=>10,'invoicePeriod'=>1,'currency'=>'GHC','paymentDescription'=>'CashPayment','methodofPayment'=>'CASH','productCollection'=>'PRCC4');
$certFile = "certs/Entrust.pem";
$options = array('soap_version' => SOAP_1_1 , 'local_cert' => $certFile , 'exceptions' => true ,'trace' => true ,'wdsl_local_copy' => true ,'ssl' => array('verify_peer' => false) ,'https' => array('curl_verify_ssl_peer' => false, 'curl_verify_ssl_host' => false)
);
try
{
echo "SOAP Client Object Made <br />";
//To Make soap client using WSDL files offline
$client = new SoapClient("RTPP_Web_Service_WSDL_20130306/multichoice.paymentservice.wsdl",$options);
}
catch(SoapFault $E)
{
echo "Error:--> ".$E->faultstring;
}
print_r($client->__getFunctions());
try
{
echo $pmt_customer=$client->__call("SubmitPayment", $arr2)."<br /><br />";
}
catch(SoapFault $fault)
{
echo "came here in catch";
trigger_error("SOAP Fault:(faultcode: {$fault->faultcode}\n"."faultstring: {$fault->faultstring})", E_USER_ERROR);
}
I have go through all WSDL files i am using and got all elements, messages, operations, parameters etc. In one of the WSDL file soap address is as follows:
<soap:address location="https://wispqa.multichoice.co.za/PaymentServicePerimeter/Intermediate.svc" />
which is actual address of service being called, normally services are called with ?WSDL at the end of url but even after adding this at the end of above url the service page appearance remains same.
One thing in service documentation is written as "The service currently does not make use of message contracts."
I am sending request to service calling one of its methods from the list that i got by calling "$client->__getFunction()". But instead of response it is giving fatal error that can be seen from the screenshot at http://i.stack.imgur.com/GvS3d.png.
I have been working on it for a almost a week but got stuck here. Please if anybody can get me out of here. Thanks in advance.
I got my answer, What i was missing is soap action for the method to be called. I have given soap action by visiting WSDL files thoroughly and found soap action for method i was calling as:
$submitpayment_action = "http://MultiChoice.PaymentService/ServiceContracts/PaymentServiceContract/SubmitPayment";
Also i have changed format of my request by using xml format whose excerpt is as follows(i have got this xml format by using SOAPUI tool):
$submitpay = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"
xmlns:mes=\"http://MultiChoice.PaymentService/MessageContracts\"
xmlns:ser=\"http://MultiChoice.PaymentService/ServiceContracts\"
xmlns:dat=\"http://MultiChoice.PaymentService/DataContracts\"
xmlns:mcom=\"http://mcomp.scontracts\" xmlns:mcom1=\"http://mcomp.dcontracts\">
<soapenv:Header/>
<soapenv:Body>
<mes:SubmitPaymentRequest>
<ser:PaymentRequest>
<dat:paymentVendorCode>".$vendorcode."</dat:paymentVendorCode> .......
and finally using this "__doRequest" SOAP method to call service method as:
$response = $client->__doRequest($submitpay,$location,$submitpayment_action,SOAP_1_1,$one_way = 0);
print_r($response);
This show me the desired response. Now one can extract required information from the response using "simplexml_load_string($response);" , "registerXPathNamespace()" and "xpath('//string');" methods.
I'm having some problems connecting to MS web service hosted at appharbor.com
Here's the php code:
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE
);
try {
$client = new SoapClient("http://ppjparsanje.apphb.com/Prazniki.asmx?wsdl", $options);
print_r($client->__call("get_praznike", array("json")));
print_r($client->get_praznike("json"));
print_r($client->__soapCall("get_praznike", array("json"), array('soapaction' => 'http://ppjparsanje.apphb.com/get_praznike')));
} catch(Excteption $e) {
echo $e->getMessage()."<br />";
}
The error is:
Fatal error: Uncaught SoapFault exception: [HTTP] Could not connect to host
Which is strange because service seems to work fine. I have also tried to add port 14275, but no luck. Any suggestions?
Thanks in advance!
you have made a mistake the soap has version 1 na your are passing 1.2 in options then how will it connect
$soapClient = new SoapClient('http://ppjparsanje.apphb.com/Prazniki.asmx?wsdl', array('cache_wsdl' => WSDL_CACHE_NONE) );
print_r($soapClient);
i get output:- hence due to bad code you are getting error
SoapClient Object ( [_soap_version] => 1 [sdl] => Resource id #3 )
well here i have acceses uk mail api
take a look at how i have called a method its bit diffrent
but u will get an idea
php soap client for uk mail webservice api?
I need to connect to a web service that requires authentication credentials in the form of a plain text user name and password.
I have a basic understanding of SOAP and have managed to connect to other open web services that do not require a username or password using NuSOAP.
The following was sent to me:
<?php
// Set up security options
$security_options = array("useUsernameToken" => TRUE);
$policy = new WSPolicy(array("security" => $security_options));
$security_token = new WSSecurityToken(array(
"user" => "xxx",
"password" => "xxx",
"passwordType" => "basic"));
// Create client with options
$client = new WSClient(array("wsdl" => "https://xxx.asmx?wsdl",
"action" => "http://xxx",
"to" => "https://xxx",
"useWSA" => 'submission',
"CACert" => "cert.pem",
"useSOAP" => 1.1,
"policy" => $policy,
"securityToken" => $security_token));
// Send request and capture response
$proxy = $client->getProxy();
$input_array = array("From" => "2010-01-01 00:00:00",
"To" => "2010-01-31 00:00:00");
$resMessage = $proxy->xxx($input_array);
?>
After some research I understand that the above implementation uses wso2. I need to be able to do this without using wso2.
I have tried my best to look for resources (Google, forums, etc) about the above but haven't been able to find anything. I have read some tutorials on SOAP and have been able to set up a SOAP client using PHP but cannot get my head around all the authentication and "policies".
An explanation of how to achieve this and maybe some links to further reading about this would be very much appreciated as I am tearing my hair out! Ideally I would like some links to resources for an absolute beginner to the SOAP authentication.
Thanks. P.S some of the links/credentials in the above could have been xxx'd for privacy.
If you have the SOAP extension enabled in php (php version >= 5.0.1), you can use the SoapClient class to process your request. To authenticate, you can pass the username and password to the class with the target URL:
$soapURL = "https://www.example.com/soapapi.asmx?wsdl" ;
$soapParameters = Array('login' => "myusername", 'password' => "mypassword") ;
$soapFunction = "someFunction" ;
$soapFunctionParameters = Array('param1' => 42, 'param2' => "Search") ;
$soapClient = new SoapClient($soapURL, $soapParameters);
$soapResult = $soapClient->__soapCall($soapFunction, $soapFunctionParameters) ;
if(is_array($soapResult) && isset($soapResult['someFunctionResult'])) {
// Process result.
} else {
// Unexpected result
if(function_exists("debug_message")) {
debug_message("Unexpected soapResult for {$soapFunction}: ".print_r($soapResult, TRUE)) ;
}
}
If you're not sure about the functions you can call, you can view the target URL (e.g. ending in ".asmx?wsdl") in your browser. You should get an XML response that tells you the available SOAP functions you can call, and the expected parameters of those functions.
Check out the soap_wsse library