Using Guzzle to consume SOAP - soap

I'm loving the Guzzle framework that I just discovered. I'm using it to aggregate data across multiple API's using different response structures. It's worked find with JSON and XML, but one the services i need to consume uses SOAP. Is there a built-in way to consume SOAP services with Guzzle?

You can get Guzzle to send SOAP requests.
Note that SOAP always has an Envelope, Header and Body.
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<NormalXmlGoesHere>
<Data>Test</Data>
</NormalXmlGoesHere>
</soapenv:Body>
The first thing I do is build the xml body with SimpleXML:
$xml = new SimpleXMLElement('<NormalXmlGoesHere xmlns="https://api.xyz.com/DataService/"></NormalXmlGoesHere>');
$xml->addChild('Data', 'Test');
// Removing xml declaration node
$customXML = new SimpleXMLElement($xml->asXML());
$dom = dom_import_simplexml($customXML);
$cleanXml = $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);
We then wrap our xml body with the soap envelope, header and body.
$soapHeader = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>';
$soapFooter = '</soapenv:Body></soapenv:Envelope>';
$xmlRequest = $soapheader . $cleanXml . $soapFooter; // Full SOAP Request
We then need to find out what our endpoint is in the api docs.
We then build the client in Guzzle:
$client = new Client([
'base_url' => 'https://api.xyz.com',
]);
try {
$response = $client->post(
'/DataServiceEndpoint.svc',
[
'body' => $xmlRequest,
'headers' => [
'Content-Type' => 'text/xml',
'SOAPAction' => 'https://api.xyz.com/DataService/PostData' // SOAP Method to post to
]
]
);
var_dump($response);
} catch (\Exception $e) {
echo 'Exception:' . $e->getMessage();
}
if ($response->getStatusCode() === 200) {
// Success!
$xmlResponse = simplexml_load_string($response->getBody()); // Convert response into object for easier parsing
} else {
echo 'Response Failure !!!';
}

IMHO Guzzle doesn't have full SOAP support and works only with HTTP requests.
src/Guzzle/Http/ClientInterface.php Line:76
public function createRequest(
$method = RequestInterface::GET,
$uri = null,
$headers = null,
$body = null,
array $options = array()
);
Even if SOAP server is configured to negotiate on port 80 I think php SoapClient is more appropriate solution here as it supports WSDL

Old Topic, but as I was searching for the same answer, it seems async-soap-guzzle is doing the job.

Guzzle HTTP can be used for SOAP requests & works like a charm:
Below is the way I have implemented.
Create variables:
public function __construct(Request $request) {
$this->request = $request;
$this->openSoapEnvelope = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">';
$this->soapHeader = '<soap:Header>
<tem:Authenticate>
<-- any header data goes here-->
</tem:Authenticate>
</soap:Header>';
$this->closeSoapEnvelope = '</soap:Envelope>';
}
Create a function to form a soap request.
public function generateSoapRequest($soapBody){
return $this->openSoapEnvelope . $this->soapHeader . $soapBody . $this->closeSoapEnvelope;
}
Define a body & call generateSoapRequest method.
e.g:
$soapBody = '<soap:Body>
<tem:GetSomeDetails/>
</soap:Body>';
$xmlRequest = $this->generateSoapRequest($soapBody);
$client = new Client();
$options = [
'body' => $xmlRequest,
'headers' => [
"Content-Type" => "text/xml",
"accept" => "*/*",
"accept-encoding" => "gzip, deflate"
]
];
$res = $client->request(
'POST',
'http://your-soap-endpoint-url',
$options
);
print_r($res->getBody());

Related

JWT: Why am I always getting token_not_provided?

I am sending a PUT request to an API endpoint I have created. Using jwt, I am able to successfully register and get a token back.
Using Postman, my request(s) work perfectly.
I am using Guzzle within my application to send the PUT request. This is what it looks like:
$client = new \Guzzle\Http\Client('http://foo.mysite.dev/api/');
$uri = 'user/123';
$post_data = array(
'token' => eyJ0eXAiOiJKV1QiLCJhbGc..., // whole token
'name' => 'Name',
'email' => name#email.com,
'suspended' => 1,
);
$data = json_encode($post_data);
$request = $client->put($uri, array(
'content-type' => 'application/json'
));
$request->setBody($data);
$response = $request->send();
$json = $response->json();
} catch (\Exception $e) {
error_log('Error: Could not update user:');
error_log($e->getResponse()->getBody());
}
When I log the $data variable to see what it looks like, this is what is returned.
error_log(print_r($data, true));
{"token":"eyJ0eXAiOiJKV1QiL...","name":"Name","email":"name#email.com","suspended":1}
Error: Could not suspend user:
{"error":"token_not_provided"}
It seems like all data is getting populated correctly, I am not sure why the system is not finding the token. Running the "same" query through Postman (as a PUT) along with the same params works great.
Any suggestions are greatly appreciated!
The token should be set in the authorization header, not as a post data parameter
$request->addHeader('Authorization', 'Basic eyJ0eXAiOiJKV1QiL...');

Symfony2 - How to get custom header while testing REST API

I'm testing REST-API. In my DefaultTestController
$client = static::createClient();
$crawler = $client->request(
'GET',
"someurl",
[],
[],
[
'HTTP_X_AUTH_TOKEN' => $clientAuthToken,
]
);
In my REST-controller I'm waiting to get x-auth-token header
$request = Request::createFromGlobals();
$authToken = $request->headers->get('x-auth-token');
but I don't. Whats I do wrong?
In your REST-CONTROLLER you can access to the current request passing it to the methods as follow example:
public function someAction(Request $request)
{
$authToken = $request->headers->get('x-auth-token');
}
Instead of creating a new empty one.
Hope this help

OneDrive REST API error 400 when creating folder

I have a problem when trying to create a folder in my OneDrive using the REST API. I'm using following documentation page https://dev.onedrive.com/items/create.htm. I have successfully authenticated and the token is working ok on other endpoints.
I spent now over a day trying every possible URI/method combination on this one but with no success. All other endpoints (directory listing etc.) are OK, just this one is driving me crazy.
If anyone could point me to the error in my approach, any help would be appreciated.
The code below returns error 400 with following message:
{"error":{"code":"invalidRequest","message":"The request is malformed or incorrect."}}
I'm using GuzzlePhp library for the request handling. My code (simplified):
$parentId = '01WZZ7ZY2LNHB75JADQJD3GGUQFSCRRZTQ'; //id to root
$method = "POST";
//none of these does the trick (to be clear, I use only one at the time)
$url = '/_api/v2.0/drive/items/'.$parentId.'/NewFolder'; //put
$url = '/_api/v2.0/drive/items/'.$parentId.'/children'; //put
$url = '/_api/v2.0/drive/items/'.$parentId.'/children'; //post
$url = '/_api/v2.0/drive/root:/NewFolder'; //post
$options = [
'headers' => [
'Authorization' => $token,
'Content-Type' => 'application/json',
'Content-Length'=> 0,
]
'form_params' => [
"name" => "NewFolder",
"folder" => (object)[],
"#name.conflictBehavior" => "fail"
]
];
//Guzzle library sends the code as specified
$res = $this->client->request($method, $url, $options);
The OneDrive API doesn't support form post semantics - the parameters are expected in the body as a JSON encoded blob. I haven't used Guzzle, but something like this should work:
$parentId = '01WZZ7ZY2LNHB75JADQJD3GGUQFSCRRZTQ'; //id to root
$method = "POST";
//none of these does the trick (to be clear, I use only one at the time)
$url = '/_api/v2.0/drive/items/'.$parentId.'/NewFolder'; //put
$url = '/_api/v2.0/drive/items/'.$parentId.'/children'; //put
$url = '/_api/v2.0/drive/items/'.$parentId.'/children'; //post
$url = '/_api/v2.0/drive/root:/NewFolder'; //post
$options = [
'headers' => [
'Authorization' => $token,
'Content-Type' => 'application/json',
'Content-Length'=> 0,
]
'body' =>
'{
"name": "NewFolder",
"folder": { },
"#name.conflictBehavior": "fail"
}'
];
//Guzzle library sends the code as specified
$res = $this->client->request($method, $url, $options);

SOAP Client Request --> Bad Request

I've been asked to look into syncing data using a SOAP service. I don't really know SOAP very well at all and I get a Bad Request Error.
The function I'm trying to call is a test echo function:
public string EchoAuthenticated(string text)
Each time I call it I get an error.
I have commented out the username / password setting as I don't know the username and password right now and my contact person is on leave :( Right now though I'd be perfectly happy just to get an authentication failed message rather than an error...
If anyone could point me in the right direction here please...
Thanks,
John
<?php
$apiUrl = 'https://exdev.api.propctrl.co.za/v3/Integration.svc?wsdl';
$options = array( 'trace' => 1, 'exceptions' => 1, 'soap_version' => SOAP_1_2);
try
{
$client = new SoapClient($apiUrl, $options);
//$data = array(
// 'Username' => "test",
// 'Password' => "test"
//);
//$header = new SoapHeader('https://exdev.api.propctrl.co.za/v3/', 'CredentialsHeader', $data, false);
//$client->__setSoapHeaders($header);
var_dump($client->__getFunctions());
print $client->EchoAuthenticated("Test String");
var_dump($client->__getLastRequest());
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
You might try something like:
...
$client = new SoapClient($apiUrl, $options);
var_dump($client->__getFunctions());
$auth = array("Username" => "John", "Password" => "secret",
"IsP24Credentials" => false);
$header = new SoapHeader("https://www.propctrl.com/", "CredentialsHeader",
$auth, FALSE);
$client->__setSoapHeaders($header);
print $client->EchoAuthenticated(array(
"text" => "My text to be echoed."
));
var_dump($client->__getLastRequest());
...
This should result in a request SOAP request like this:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="https://www.propctrl.com/v3" xmlns:ns2="https://www.propctrl.com/">
<env:Header>
<ns2:CredentialsHeader>
<ns2:IsP24Credentials>false</ns2:IsP24Credentials>
<ns2:Password>secret</ns2:Password>
<ns2:Username>John</ns2:Username>
</ns2:CredentialsHeader>
</env:Header>
<env:Body>
<ns1:EchoAuthenticated>
<ns1:text>My text to be echoed.</ns1:text>
</ns1:EchoAuthenticated>
</env:Body>
</env:Envelope>
As a side note, you might have a look at http://www.soapui.org/. This tool helps greatly with web service development.

Getting Response Body using Zend_http_Client

I am succesfully calling a REST API with the following code
$client = new Zend_Http_Client();
$client->setMethod(Zend_Http_Client::POST);
$client->setUri('http://www.example.com/api/type/');
$client->setParameterPost(array(
'useremail' => '******#*****.***',
'apikey' => 'secretkey',
'description' => 'TEST WEB API',
'amount' => '5000.00'
));
However I would like to get both the header value-(201) and Response Body that are returned after the execution.
How do I proceed with that?
I am assuming that you're actually executing the request via:
$response = $client->request();
At that point all you need is in the $response object,
//Dump headers
print_r($response->headers);
//Dump body
echo $response->getBody();
Refer to the Zend_Http_Response docs at:
http://framework.zend.com/apidoc/1.10/
for more methods that are available.
this should work...
$client->setUri ( $image_source_urls );
$response = $client->request ( 'GET' );
$folder_content = $response->getBody ();