Send form via SOAP with PHP

Asked

Viewed 101 times

-3

Hello, all good?

I am very beginner in PHP, and I am integrating a client’s CRM with a form for capturing Leads. Their system is small in scope, and the only thing I have to help me with is a document with some instructions - in addition to searches here in the OS and on the Internet.

On Google, I found some solutions for validation, necessary step before sending the form. I tested and everything worked out - I am working with the following code:

<?php
$client = new SoapClient('http://crm4u.azurewebsites.net/WS_Integracao.asmx?WSDL');

$function = 'GetToken';

$arguments= array('GetToken' => array(
                  'ApiKey'   => XXXXXXXX
                ));

$options = array('location' => 'http://crm4u.azurewebsites.net/WS_Integracao.asmx');

$result = $client->__soapCall($function, $arguments, $options);

$json = $result->GetTokenResult;
$item = json_decode($json, true);

print_r($item);

$apikey = 'XXXXXXXX';
$apipassword = 'YYYYYYYY';

$combinacao = $apikey."|".$apipassword."|".$item;

$combinacaomd5 = md5($combinacao);

$tokenfinal = $combinacaomd5."|".$apikey;

print_r($tokenfinal);

?>

Now that I have the token validated for sending the form, I need to understand how to send the data of a form in the following format:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <soapenv:Header/>
    <soapenv:Body>
        <tem:PutLead>
            <tem:pessoa>
                <tem:Nome>Nome do lead</tem:Nome>
                <tem:Email>[email protected]</tem:Email>
                <tem:Telefone>11 99999-9999</tem:Telefone>
                <tem:Observacoes>Observações do lead</tem:Observacoes>
            </tem:pessoa>
            <tem:Key>TOKEN GERADO PELO CÓDIGO ANTERIOR</tem:Key>
        </tem:PutLead>
    </soapenv:Body>
</soapenv:Envelope>

If the request is successfully completed, we should get the return of the lead ID - as code below:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <PutLeadResponse xmlns="http://tempuri.org/">
            <PutLeadResult>ID DO LEAD</PutLeadResult>
        </PutLeadResponse>
    </soap:Body>
</soap:Envelope>

I have done a lot of research, but I haven’t found anything relevant on the subject - I don’t even know if it’s the best way to do this. I preferred to follow with PHP because I have some affinity. Someone can give me a light?

Thank you very much!

1 answer

-1

I read the material divided by Augusto, and some things I found on the internet - I arrived in the following:

<?php

$nome = $_POST['nome'];
$email = $_POST['email'];
$telefone = $_POST['telefone'];
$observacoes = $_POST['observacoes'];


$client = new SoapClient('http://crm4u.azurewebsites.net/WS_Integracao.asmx?WSDL');

$function = 'GetToken';

$arguments= array('GetToken' => array(
                                        'ApiKey'   => XXXXXXXXXXX
                                      )
                 );

$options = array('location' => 'http://crm4u.azurewebsites.net/WS_Integracao.asmx');

$result = $client->__soapCall($function, $arguments, $options);

$json = $result->GetTokenResult;
$item = json_decode($json, true);

print_r($item);

$apikey = 'XXXXXXXXXXXXXXX';

$apipassword = 'XXXXXXXXXXXXXXXXX';

$combinacao = $apikey."|".$apipassword."|".$item;

$combinacaomd5 = md5($combinacao);

$tokenfinal = $combinacaomd5."|".$apikey;

print_r($tokenfinal);

$argumentsPost = array('PutLead' => array(
                            'Pessoa'   => array(
                                    'Nome'   => $nome,
                                    'Email'   => $email,
                                    'Telefone'   => $telefone,
                                    'Observacoes'   => $observacoes
                            ),
                            'Key' => array(
                                'ApiKey'   => $tokenfinal
                              )
                            )
                        );

$optionsPost = array('location' => 'http://crm4u.azurewebsites.net/WS_Integracao.asmx');

$functionPost = 'PutLead';

$resultPost = $client->__soapCall($functionPost, $argumentsPost, $optionsPost);

?>

Form code:

<form id="formulario" action="https://superaguaruja.com.br/crm4u/apiguaruja.php" method="post">
<div width="100%">
    <input type="text" id="nome" name="nome" class="campos" placeholder="Seu nome" required>
</div>
<div width="100%">
    <input type="text" id="email" name="email" class="campos" placeholder="Seu e-mail" required>
<div width="100%">
    <input type="tel" id="telefone" name="telefone" class="campos" placeholder="Seu celular" required>
</div>    
<div width="100%" class="motivo">
    <select id ="observacoes" name="observacoes">
    <option value="Selecionar">----- Com o que podemos te ajudar?</option>
    <option value="Concurso e vestibular">Concurso e vestibular</option>
    <option value="Inteligência">Inteligência</option>
    <option value="Memória">Memória</option>
    <option value="Profissional">Profissional</option>
    <option value="Alzheimer">Alzheimer</option>
    <option value="Ansiedade">Ansiedade</option>
    <option value="Atenção">Atenção</option>
    <option value="Estresse">Estresse</option>
    <option value="Estudo">Estudo</option>
    </select>
</div>
<div width="100%" align="center">
    <button id="enviar" name="enviar" type="submit" class="btn-enviar">Enviar</button>
</div>
</form>

When I send, the following error appears:

Fatal error: Uncaught Soapfault Exception: [Soap:Server] Server was Unable to process request. ---> Index was Outside the Bounds of the array. in /home2/supera48/guaruja_root/crm4u/apiguaruja.php:58 Stack trace: #0 /home2/supera48/guaruja_root/crm4u/apiguaruja.php(58): SoapClient->__soapCall('PutLead', Array, Array) #1 {main} thrown in /home2/supera48/guaruja_root/crm4u/apiguaruja.php on line 58

Is it a mistake in the way I put the arrays, or in the last string? Can anyone help me?

Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.