SOAP client.php

Asked

Viewed 1,138 times

1

Good morning. I’m trying to mess with SOAP Web Service but I’m having problems. When I run client.php in the browser it doesn’t work. Does anyone know how to solve?

php server.

<?php

$options = array('uri' => 'http://alairrepresentacoes-com-br.umbler.net/');
$server = new SoapServer(null, $options);

$server->setClass('MeuSoapServer');

$server->handle();

class MeuSoapServer {

    public function mensagem($nome)
    {
        return "Boas Vindas $nome !";
    }

    public function soma($a, $b)
    {
        return $a + $b;
    }
}
?>

client.php

<?php

$options = array(
    'location' => 'http://alairrepresentacoes-com-br.umbler.net/servidor.php',
    'uri' => 'http://alairrepresentacoes-com-br.umbler.net/'
);

$client = new SoapClient(null, $options);


echo $client->mensagem('Douglas') . "<br />";
echo $client->soma(3, 5) . "<br />"

?>

Thank you in advance.

2 answers

1


Missing one ; at the end of:

echo $client->soma(3, 5) . "<br />"

Should be:

echo $client->soma(3, 5) . "<br />";

Then this error may be occurring:

Parse error: syntax error, unexpected end of file, expecting ',' or ';' in client.php on line 12

However the exposure of errors must be off on your site, soon you will only receive the message 500 Internal Error Server.

Also note that your server "maybe" is not with the extension Soap for PHP enabled on php.ini and because possibly the errors are turned off on your server you cannot notice the error messages, such as:

Fatal error: Class 'Soapclient' not found in client.php on line 8

And this

Fatal error: Class 'Soapserver' not found in server.php on line 17

Note that in the servidor.php you can omit the ?> from the end, as it is optional and will help avoid blank space in SOAP XML, which may cause problems.

Enable SOAP extension in PHP

To do this you need access to php.ini from your server, usually via Cpanel or SSH, if you do not have access is because your hosting (if it is a hosting) does not allow, then you will have to talk to your technical support, however if you have access just search inside the php.ini the following line:

  • If it is a server Unix-like (linux for example)

    ;extension=soap.so
    
  • If it is a server Windows Server

    ;extension=php_soap.dll
    

Then remove the ; of the front leaving so:

extension=soap.so

Or if it’s Windowsserver, like this:

extension=php_soap.dll

And then reboot the server, however if you have control over the server reboot only Apache or Ngnix or IIS (depends on the type of server you use), then it should work.

Note that I tested your code, and had to activate on my local server the Soap by php.ini, after activating I had this result:

Welcome Douglas !
8

That is to say 8 is the sum of soma(3, 5).

  • Thank you very much, I don’t know how I forgot the semicolon kkk

  • Your reply was very complete, it helped a lot. I would just like to ask about Uri and Location? They are right so?

  • @Viníciusasisneves as per the example of the documentation http://php.net/manual/soapclient.soapclient.php#example-6247 is correct yes yes.

0

If someone has a similar problem, try Guilherme’s answer above, if you can’t handle php.ini the way he said it (as in my case it was already right in php.ini), remove all the lines from the.php server after php ?>. I mean, all the blank lines, for some reason this influences.

  • You can omit the ?> if there is no more content below, in php it is opitional and helps to avoid extra space.

  • I took advantage and added the explanation in the reply: Note que no servidor.php você pode omitir o ?> do final, pois ele é opcional e ajudará a evitar espaço em branco no XML do SOAP, que podem causar problemas..

  • 1

    I didn’t know it was optional, but good to know. But your reply was very helpful, thank you. Once I have more reputation here I come back to evaluate, need at least 15 to evaluate kkk

Browser other questions tagged

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