How to create SOAP request in PHP using SOAPClient with authorization? - php

I have to integrate one soap request in my php laravel .
For that I have some basics information regarding SOAP request i.e
URL
WSDL
HEADERS
now i have tested this soap request in postman it was completely okay
but the problem is when i am trying to do the same on php using SOAP client i am getting the error
here is my code
$options = [
'cache_wsdl' => 0,
'trace' => 1,
'stream_context' => stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
]]),
];
$soapClient = new SoapClient("https://ibluatapig.indusind.com/app/uat/DomesticPayService1", $options);
$headers = new \SoapHeader($nameSpace, 'RequestorCredentials', [
'Content-Type' => 'text/xml',
'SOAPAction' => 'http://tempuri.org/IDomesticPayService/GetAccBalance',
'X-IBM-Client-Secret' => 'secret-key',
'X-IBM-Client-ID' => 'client-id',
]);
$soapClient->__setSoapHeaders($headers);
dd($soapClient);
and the error is
I think my problem is occuring because i am not able to send $namespace for soapHeaders also maybe there is some issue on headers part so please any idea or help why this is happening ??
Thank you !

Related

Problem with communication with web service

For the past few days I've been trying to connect to web service with soap. This is the configurations that I try to pass to SOAP:
$config['soap_parameters'] = [
'cache_wsdl' => 0,
'trace' => 1,
'stream_context' => stream_context_create(
[
'ssl' => [
'verify_peer' => 0,
'verify_peer_name' => 0,
'allow_self_signed' => 1,
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
]
]
),
'login' => 'Username',
'password' => 'Password'
];
$this->configureSoapClient($config, $container);
When I try to sync web service client, I get an error saying Unable to dump a service container if a parameter is an object or a resource. When I've tried to look where's the issue, I've found out that stream_context_create was the problem, because it returns resource and error states, that it doesn't like when you give object or resource. I tried to look for alternative for stream_context_create method, but, unfortunately, seems that there's no alternative for it. Maybe some of you encountered problem like this and have a solution for this? Thank you in advance.

PHP SoapClient Timeout in Different Enviroments

I'm working in two differents enviroments, in local machine and production.
I did a soap request using soap ui with local url and request looks great (after 4/5min), that's not happen in production (with production url), after 2min a get a message "connection reset".
$soap = new Zend_Soap_Server ( $url_wsdl,
array (
'soap_version' => SOAP_1_2,
'trace' => true,
'connection_timeout' => 300, "use" => SOAP_ENC_ARRAY,
'encoding' => 'ISO8859-1',
'ssl' => array(
// set some SSL/TLS specific options
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
)
);
I check some timeouts and php.ini and i didn't found not wrong. Anyone have some idea to help me?
Thank you!

Connecting to dev magento soap api v1 with self-signed certificate - wrong version error

I need to connect to Magento SOAP API v1 dev server, which is running over https using self-signed ssl certificate.
Given my soap api url is: https://my-store.com/index.php/api/soap/?wsdl
The traditional way of initiating a soap client like this:
$client = new SoapClient($soap_api_url);
This produces the following error:
SOAP-ERROR: Parsing WSDL: Couldn't load from
'https://my-store.com/index.php/api/soap/?wsdl'
I figured the error must be due to self-signed certificate, so I tried a different approach like this:
$context = stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);
$client = new SoapClient(null, [
'location' => $soap_api_url,
'uri' => 'urn:Magento',
'stream_context' => $context
]);
This no longer complains about not being able to load the WSDL, now returns this error:
Wrong Version
I think the problem is with the uri in the connection option; any idea how to tell this code I want to connect to v1 of the magento soap api?
I've fixed it like this:
$context = stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);
$client = new SoapClient($soap_api_url, [
'stream_context' => $context
]);

SOAP Client over HTTPS with SSL certificates on both sides

I have to develop a SOAP Client, and the supplier send me this specifications:
Will be transmited using HTTPS through IP, and will be Packaged as XML documents that adjust to the diferent defnitions of XML scheme.
The Communications is synchronous, the third party should wait for response.
Each request and response will be signed.
I'm using the soapClient class from PHP, and all works fine, except when I try to use my private key to establish communication with the server:
Code: WSDL | Message: SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://remoteserver/CustomerManagementService?wsdl' : failed to load external entity "https://remoteserver/CustomerManagementService?wsdl
Then I tried creating a .pem file, it contains my private key concatenated with my certificate, as I've read in: how to send SOAP request with SSL certificate in PHP?
But it still returns an error:
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://remoteserver:80/CustomerManager/proxy/CustomerManagementService?WSDL%2FGWTCommonResources%2Fwsdl%2FGWTCommonMessages' : failed to load external entity "http://remoteserver:80/CustomerManager/proxy/CustomerManagementService?WSDL%2FGWTCommonResources%2Fwsdl%2FGWTCommonMessages
I wonder if there is some way to get exactly the raw data that is being sent by the soapClient class of PHP. And where I must set the certificate of the supplier.
I've already tried with "$client->__getLastRequest()", but I'm getting a NULL. This is my code:
$client = new anotherSoapClient($service, array(
'local_cert' => $pem,
'style' => SOAP_RPC,
'use' => SOAP_ENCODED,
'soap_version' => SOAP_1_2,
'authentication'=> SOAP_AUTHENTICATION_DIGEST,
'ssl' => array(
'ciphers'=> "SHA1",
'verify_peer' => false,
'allow_self_signed' => true
),
'https' => array(
'curl_verify_ssl_peer' => false,
'curl_verify_ssl_host' => false
),
'cache_wsdl' => WSDL_CACHE_NONE,
'cache_ttl' => 86400,
'trace' => true,
'exceptions' => true,
));
// Test connection
echo BR.'Functions: <pre>';var_dump($client->__getFunctions());echo '</pre>';
$XMLrequest = $client->prepareRequest($email);
$response = $client->__anotherRequest('getCustomerInfo', $XMLrequest);
echo "REQUEST:\n" . $client->__getLastRequest() . "\n";
By the way, I'm using PHP 5.4.9 on my local machine and the server have PHP 5.3.10 and anotherSoapClient is a class who extend PHP soapClient class: PHP soapClient send custom XML
For debugging proposals your SOAP request you have to extend the SoapClient class.
class SoapClientDebug extends SoapClient
{
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
// Add code to inspect/dissect/debug/adjust the XML given in $request here
// Uncomment the following line, if you actually want to do the request
// return parent::__doRequest($request, $location, $action, $version, $one_way);
}
}
And next use it in your request:
$client = new SoapClientDebug("x.wsdl");
$response = $client->__soapCall($function);
echo $client->__getLastRequest();
Hope it helps to debug your code!
You probably need to specify following SoalClient options:
$defaultEndpoint = "https://remoteserver/CustomerManagementService";
$uri = "https://remoteserver";
$client = new anotherSoapClient($service, array(
'local_cert' => $pem,
'location' => $defaultEndpoint,
'uri' => $uri,
'style' => SOAP_RPC,
'use' => SOAP_ENCODED,
'soap_version' => SOAP_1_2,
'authentication'=> SOAP_AUTHENTICATION_DIGEST,
'ssl' => array(
'ciphers'=> "SHA1",
'verify_peer' => false,
'allow_self_signed' => true
),
'https' => array(
'curl_verify_ssl_peer' => false,
'curl_verify_ssl_host' => false
),
'cache_wsdl' => WSDL_CACHE_NONE,
'cache_ttl' => 86400,
'trace' => true,
'exceptions' => true,
));

PHP 5.3.6 SoapClient::__doRequest(): SSL: Connection reset by peer

I'm working on consuming a .net web service in php 5.3.6. I'm using SoapClient class to make the connection. It is keep on failing with "SoapClient::__doRequest(): SSL: Connection reset by peer" and "SoapFault Object ( [message:protected] => Error Fetching http headers ".
This is happening only for the Methods/Operations. If i use $response = $objClient->__getFunctions(); and it is working fine and I'm getting the responses with out issue.
$objClient = new SoapClient("http://sample.idws.syndication.kbb.com/3.0/VehicleInformationService.svc?wsdl", array('trace' => 1, 'username' => 'xxxxxxx', 'password' => 'xxxxxxx', 'soap_version' => SOAP_1_2, 'exceptions' => true ));
PHP: php 5.3.6 with ssl soap enabled.
OS: Ubuntu 11.10
i ve been facing a similar issue the past few months.
it turned out afterall that the problem was when i used non-wsdl mode
http://php.net/manual/en/soapclient.soapclient.php
occassionally the remote server wouldn't respond on the request of the location of the wsdl.
initial non-wsdl mode
$soapx = new SoapClient(null,
array(
"trace" => true,
'cache_wsdl' => WSDL_CACHE_NONE,
'location' => 'http://remote_wsdl_url',
'uri' => 'http://necessary_uri',
'use' => SOAP_LITERAL,
'style' => SOAP_DOCUMENT,));
turned to wsdl mode
$soapx = new SoapClient('http://remote_wsdl_url_turned_to_local',
array(
"trace" => true,
'cache_wsdl' => WSDL_CACHE_NONE,));
It seems like there is a problem on the SOAP Server end.
The best online client for debugging SOAP is soapclient
you might give it a try.
I recently came across this due to the same issue. For us the problem was with the SSL protocol being used. We had to force TLS 1.1 and everything started humming along. The key working component for us here is the 'crypto_method'.
$wsdl = 'PATH/TO/WSDL';
$url = 'http://URL_TO_SOAP_SERVICE';
$cert = 'PATH/TO/CLIENT/CERT';
$context = stream_context_create([
'ssl' => [
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT,
'verify_peer' => true,
'verify_peer_name' => true,
'allow_self_signed' => false,
'cafile' => '/path/to/cafile.selfsigned'
]
]);
$params = [
'location' => $url,
'local_cert' => $cert,
'trace' => true,
'exceptions' => true,
'verifypeer' => true,
'verifyhost' => true,
'allow_self_signed' => false,
'connection_timeout' => 180,
'keep_alive' => false,
'stream_context' => $context
];
$client = new SoapClient($wsdl, $params);

Categories