SOAP com PHP | Error: Server was Unable to process request. ---> Index was Outside the Bounds of the array

Asked

Viewed 91 times

-1

Hello, all good?

I’m a beginner in PHP and am venturing for the first time with Webservices (SOAP) - performing the integration a form I have on a Landing page with a client’s CRM. For being a platform with few customers, I have only one documentation and I’m breaking my head with this mistake.

The data sending process consists of three steps: validation (sending key and password for token generation), sending (using token and sending form information) and confirmation of the request (returns the registered lead ID - not required). The validation part has already done and worked, but sending the form is giving the following error:

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

After the token generation, I must send the form data in this following format:`

    <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 sending is confirmed, it returns me an ID - this way:

<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>

For you to view, I use and attach here the codes of the sending form and the script I am running:

FORM:

<form id="formulario" action="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>

SCRIPT (apiguaruja.php):

<?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'   => XXXXXXX
                                      )
                 );

$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 = 'XXXXXXX';

$apipassword = 'YYYYYYYY';

$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);

?>

The error, according to the script, is in the penultimate line (58). I don’t know what I can do differently, or I’m doing it incorrectly, but I suspect it may have something to do with the fact that "Pessoa" is inside "Putlead". Despite this, I have tried $functionPost calling 'Person', but the error persisted.

Does anyone have any idea how I can fix this?

Thank you very much!

1 answer

0

Hello!

I checked some things, and I realized that I had written the "Person" (tag inside the Putlead) in lower case. I changed here and now we have 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'   => XXXXXXX
                                          )
                     );

    $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 = 'XXXXXXX';

    $apipassword = 'YYYYYYYY';

    $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);

    ?>

With that, the error changed to the following:

Fatal error: Uncaught SoapFault exception: [Client] SOAP-ERROR: Encoding: object has no 'Id' property in /home2/supera48/guaruja_root/crm4u/apiguaruja.php:56 Stack trace: #0 /home2/supera48/guaruja_root/crm4u/apiguaruja.php(56): SoapClient->__soapCall('PutLead', Array, Array) #1 {main} thrown in /home2/supera48/guaruja_root/crm4u/apiguaruja.php on line 56

The error keeps referring to the last line of code, but I don’t understand what I might have done wrong. Would you have any guesses?

Thank you!

Browser other questions tagged

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