Jump to content

HTTP Request using SOAP


Sam302
 Share

Recommended Posts

I'm a student/Intern at an Agency and I was asked to test an HTTP request to a server using a code given in the servers doc which goes like this

<?php

$client = new httpClient;

$request = new http\Client\Request;

$body = new http\Message\Body;

$body->append('

<?xml version="1.0" encoding="utf-8"?>

<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> <WebServiceDiva xmlns="http://www.Divalto.fr/WebService/">

<action>&lt;action&gt;WP_CREATECLI</action>

<param>

{licence:"XXXXX=",client: {"dos":"2","nom":"Alex Xela","adr1":"53 Bd Carnot","adr2":"","adr3":"","adr4":"","cpostal":"06400","vil":"Cannes","tel":"053837979","email":"user1@devtest.com","siret":"","naf":"","regcod":"CB","tvatie":"0","catclicod":"","tacod":"","tarcod":"","blmod":"DPD","texcod1":"","texcod2":"","texcod3":"","texcod4":"","tvano":"","visa":"2"},adresse: {"nom":"Alex Xela","adr1":"53 Bd Carnot","adr2":"","adr3":"","adr4":"","cpostal":"06400","ville":"","tel":"053837979","email":"user1@devtest.com","livprincipale":"2","facprincipale":""},contact: {"contact":"Customer","nom":"Alex Xela","prenom":"","tit":"","lib":"","tel":"","telgsm":"","fax":"","email":"user1@devtest.com","servcod":"","fctcod":""},banque: {"ribcod":"","ribdo":"","iban1":"","iban2":"","iban3":""}}

</param>

</WebServiceDiva>

</soap:Body>

</soap:Envelope> ');

$request->setRequestUrl('https://divaltosainteroseline.coaxis.com/WebServiceDiva/WebServiceDiva.asmx'); $request->setRequestMethod('POST');

$request->setBody($body);

$request->setHeaders(array( 'accept-encoding' => 'gzip', 'content-type' => 'text/xml' ));

$client->enqueue($request)->send();

$response = $client->getResponse();

echo $response->getBody();

?>

 

The problem is I can't even get throught the first lines when I execute the code, It says that there is no class Http\Client or Http\Client\Message or Http\Client\Request. and I looked for these classes in php doc but couldn't find anything. also after 3 days of research I only found SaopClient and SoapCall Classes that really treat HTTP and SOAP communication but the tech guy at the server company tells me that this is the way to connect using php and I'm completely lost and need ur help

Link to comment
Share on other sites

Welcome to PHPF!

Please use the <> button to paste in code in the future.  I editted your original post to do that for you this time.

Your code is using several namespaced classes, but it is missing any statements that might require or include or autoload those classes.   The names are very generic, so it's hard to know for sure what they might be a part of.  They are not soap specific, as it is just using a generic http request class to send a request that has a soap document embedded in it.  You would expect to receive a response that had a soap document inside of it, if it actually worked.

My advice to you would be to use one of the many http libraries and just replace those calls with the ones from the library. 

You will need to have composer installed on your workstation, and hopefully you'll be able to figure out how to use it to install the http client and make a couple small changes to replace the missing libraries.

My suggestion would be to use Guzzle, mainly because it is the best known/longstanding PHP library of its type.

First off make sure you have composer installed (globally) on your machine.  In other words, follow the instructions to install it so it can be used anywhere rather than just installing it in your project directory.

Make yourself a new subdirectory.  Open a terminal and cd to that directory. Then run this:

composer require guzzlehttp/guzzle

You probably need a github account, and you may have to create a github personal access token and install it in composer.  Try to do the composer require without installing the token, but set it if you get any errors.  

If you do need the token, make one for 90 days.  The default settings are readonly, and those are fine for what you need, which is just to be able to have composer get any component libraries you might want to use.

In Github, when you create a token, it will show it to you and you can copy it to your clipboard.  Then you would run this command in the terminal:

composer config --global --auth github-oauth.github.com token_pasted_here

At that point you should have no issues with the composer require command.

It will download Guzzle libs into a vendor directory, create a composer.json file for your project, and also create an autoloader you will include.

At this point, you can take the code you were provided, and make a new version of it in the root of your project directory.  I named this file soaprequest.php, but the file name doesn't matter. 

I went ahead and ported your code to a guzzle version.  You will need to change the specific url and any token information required, as when I ran it as a test I got a 404 response for the url, so I couldn't probe further.

<?php
// soaprequest.php
require("vendor/autoload.php");

$client = new GuzzleHttp\Client([
    'base_uri' => 'https://divaltosainteroseline.coaxis.com/WebServiceDiva', 
    'timeout'  => 5.0,
    'headers' => ['accept-encoding' => 'gzip', 'content-type' => 'text/xml']
]);
$body = '<?xml version="1.0" encoding="utf-8"?>
<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> <WebServiceDiva xmlns="http://www.Divalto.fr/WebService/">
<action>&lt;action&gt;WP_CREATECLI</action>
<param>
{licence:"XXXXX=",client: {"dos":"2","nom":"Alex Xela","adr1":"53 Bd Carnot","adr2":"","adr3":"","adr4":"","cpostal":"06400","vil":"Cannes","tel":"053837979","email":"user1@devtest.com","siret":"","naf":"","regcod":"CB","tvatie":"0","catclicod":"","tacod":"","tarcod":"","blmod":"DPD","texcod1":"","texcod2":"","texcod3":"","texcod4":"","tvano":"","visa":"2"},adresse: {"nom":"Alex Xela","adr1":"53 Bd Carnot","adr2":"","adr3":"","adr4":"","cpostal":"06400","ville":"","tel":"053837979","email":"user1@devtest.com","livprincipale":"2","facprincipale":""},contact: {"contact":"Customer","nom":"Alex Xela","prenom":"","tit":"","lib":"","tel":"","telgsm":"","fax":"","email":"user1@devtest.com","servcod":"","fctcod":""},banque: {"ribcod":"","ribdo":"","iban1":"","iban2":"","iban3":""}}
</param>
</WebServiceDiva>
</soap:Body>
</soap:Envelope>';


try {
    $response = $client->request('POST', '/WebServiceDiva.asmx', ['body' => $body]);
    echo "Successful request" . PHP_EOL;
    $responseBody = $response->getBody();
    echo $responseBody;

} catch (ClientException $e) {
    echo Psr7\Message::toString($e->getRequest());
    echo Psr7\Message::toString($e->getResponse());
}

 

Again from the terminal, you will run this in the root directory of your project:

php -f soaprequest.php

It will output any errors or the response from the soap server to the screen in the terminal.  If you actually get a response, then you might want to load that data into an object using simplexml or possibly use a soap library, but the code you started with did none of those things.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.