PHP SOAP call content type - php

I'm attempting to connect with a WS via Soap and struggling with a content type error when I call the method: content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'
Below is my code, reducing number of params and hiding url, user and password.
The error happens when I call the ClientRegist() function. I'm convinced I have to pass $params in a different way, but can't find an example anywhere.
$url = 'http://---';
$ctx_opts = array(
'http' => array(
'header' => 'Content-Type: application/soap+xml'
),
'username' => '---',
'password' => '---',
'trace' => true,
'exceptions' => true
);
$ctx = stream_context_create($ctx_opts);
$client = new SoapClient($url, array('stream_context' => $ctx));
$params = array(
'Cardnumber' => $number,
'Name' => $name
);
try {
$client = new SoapClient($url, array('trace' => $trace, 'exceptions' => $exceptions));
$response = $client->ClientRegist($params);
}
catch (Exception $e) {
echo "Error!<br>";
echo $e -> getMessage ().'<br>';
echo 'Last response: '. $client->__getLastResponse();
}
var_dump($response);

Try SOAP version 1.2. Its default Content-Type is application/soap+xml
<?php
// ...
$client = new SoapClient(
$url,
array(
'soap_version' => SOAP_1_2, // !!!!!!!
'stream_context' => $ctx
)
);

Related

GDMS (Grandstream) API issue when HTTP POST curl php

I'm trying to send a HTTP POST request through curl in php to the new Grandstream API and I'm getting the same error each time. I have to say I'm currently able to do GET requests successfully but the problems start when a body has to be send in POST.
$gdms_domain = "eu.gdms.cloud";
$timestamp = round(microtime(true)*1000);
$params_data = array(
'access_token' => '*********',
'client_id' => '*****',
'client_secret' => '************',
'timestamp' => $timestamp
);
$body_data = array(
'pageSize' => "",
'pageNum' => "",
'order' => "",
'type' => "",
'orgId' => ""
);
$params = http_build_query($params_data);
$body = json_encode($body_data);
$signature = hash("sha256","&".$params."&");
$payload_data = array(
'access_token' => $token,
'signature' => $signature,
'timestamp' => $timestamp
);
$payload = http_build_query($payload_data);
$ch = curl_init();
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_URL => 'https://'.$gdms_domain.'/oapi/v1.0.0/device/list'."?".$payload,
CURLOPT_HTTPHEADER => array(
"Content_type: application/json"
)
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if(curl_error($ch)){
echo 'Request Error:'.curl_error($ch);
}else{
$response = json_decode($response);
print_r($response);
return $response;
}
curl_close($ch);
The error that is returned is:
(
[data] =>
[msg] => bad signature
[retCode] => 40003
)
The API documentation is in here GDMS API. I tried to build the signature in both possible ways according to the docs and send the body in different ways.
Thank you
You should add the hashed body to the signature.
$body_signature = hash("sha256",$body);
Then final signature should be
$signature = hash("sha256","&".$params."&".$body_signature."&");

SoapClient PHP application/soap+xml;

How to set the content type so that the following code does not return an error:
Cannot process the message because the content type 'application/soap+xml; charset=utf-8; action="http://webservice.comarchedi.com/ComarchEdiWebService/SubscriptionGet"' was not the expected type 'text/xml; charset=utf-8'.
$url = 'https://edi.edoc-online.com/EdiWebService/EdiWebService.svc?wsdl';
$stream_context_opts = [
'http' => [
'method' => 'GET',
'header' => 'application/soap+xml; charset=utf-8'
]
];
$soap_stream_context = stream_context_create($stream_context_opts);
try {
$client = new SoapClient($url, [
'soap_version' => SOAP_1_2,
'trace' => true,
'encoding' => 'UTF-8',
'stream_context' => $soap_stream_context
]);
debug($client->__getFunctions());
debug($client->SubscriptionGet($login, $password));
} catch (\Exception $ex) {
echo $ex->getMessage();
}

How to prevent http redirects when using SoapClient?

I am using standard SoapClient class to make requests to the payment server.
$context = stream_context_create([
'http' => [
'follow_location' => 0,
],
]);
$options = [
'uri' => 'urn:PaymentServer',
'location' => 'http://localhost:8001/index.php',
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'trace' => true,
'soap_version' => SOAP_1_1,
'stream_context' => $context,
'connection_timeout' => 50,
];
$soapClient = new SoapClient(null, $options);
try {
var_export($soapClient->GetPayment([]));
} catch (Throwable $e) {
echo $e->getMessage();
var_export($soapClient->__getLastResponse());
}
Script http://localhost:8001/index.php contains following code
header('Location: http://example.com', true, 302);
exit;
I expect empty result, but the actual output contains HTML from example.com
I have tried get_file_contents function with the same stream context:
var_export(file_get_contents('http://localhost:8001/index.php', false, $context));
And I have got empty string.
What SoapClient or stream context option should be set to prevent redirects?

Error on trying to connect to SOAP service

I'm trying to consume the "search" function of the Brazilian CADSUS service, but I get the following error:
"Forced circuit exception";
Right below, I have the following code, which uses Laravel 5.6 and PHP 7.1
Route::get('/ws/teste', function () {
try {
$opts = array(
'http' => array(
'user_agent' => 'PHPSoapClient'
)
);
$context = stream_context_create($opts);
$wsdlUrl = 'https://servicoshm.saude.gov.br/cadsus/CadsusService/v5r0?wsdl';
$soapClientOptions = array(
'trace' => 1,
'stream_context' => $context,
'cache_wsdl' => WSDL_CACHE_NONE,
'Username' => '*****',
'Password' => '*****'
);
$client = new SoapClient($wsdlUrl, $soapClientOptions);
$parameters = array(
'CNESUsuario' => array(
'CNES' => '6963447',
'Usuario' => 'LEONARDO',
'Senha' => ''
),
'FiltroPesquisa' => array(
'nomeCompleto' => 'SERGIO ARAUJO CORREIA LIMA',
'tipoPesquisa' => 'IDENTICA'
),
'higienizar' => 0
);
$result = $client->pesquisar($parameters);
print_r($result);
}
catch(\Exception $e) {
echo $e->getMessage();
} });
I would appreciate any help. Thanks in advance
From SOAP response:
[cvc-minLength-valid: Value '' with length = '0' is not facet-valid with
respect to minLength '1' for type '#AnonType_SenhaCNESUsuarioType'.,
cvc-type.3.1.3: The value '' of element 'ns1:Senha' is not valid
So, Senha can't be empty string.

Bigcommerce API - Creating Webhooks - Invalid Header

I'm making small steps into this project I am working on. Now creating and registering a webhook. I'm getting the below response:
400 - Invalid Header
I have tried the following code:
// Send a request to register a web hook
$http2 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
'X-Custom-Auth-Header' => $access_token,
)
)
));
$request = $http2->post('/'.$store_hash.'/v2/hooks', null, array(
'scope' => 'store/order/*',
'destination' => 'https://example.com/process_order.php',
'is_active' => true
));
$response = $request->send();
$body = $response->getBody(true);
var_dump($body);
echo '<p>Status Code: ' . $response->getStatusCode() . '</p>';
... and
// Send a request to register a web hook
$http2 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
)
)
));
$request = $http2->post('/'.$store_hash.'/v2/hooks', null, array(
'scope' => 'store/order/*',
'headers' => array(
'X-Custom-Auth-Header' => $access_token,
),
'destination' => 'https://example.com/process_order.php',
'is_active' => true
));
$response = $request->send();
$body = $response->getBody(true);
var_dump($body);
echo '<p>Status Code: ' . $response->getStatusCode() . '</p>';
I am working with the documentation here:
https://developer.bigcommerce.com/api/stores/v2/webhooks#create-a-hook
However, I can't seem to work out what {secret_auth_password} is as well? The documentation doesn't explain this. I am sending the Client ID and Client Header as part of the headers as well.
Still getting Invalid Header as a response.
I am using Guzzle.
Can anyone assist me on this please?
I have finally worked out what I did wrong after numerous attempts.
Answer: send the data in JSON format.
Resolved Code:
// Send a request to register a web hook
$http3 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
)
)
));
$request = $http3->post('/'.$store_hash.'/v2/hooks', null, json_encode(array(
'scope' => 'store/order/statusUpdated',
'destination' => 'https://example.com/process_order.php',
)));
$response = $request->send();

Categories