SOAP client.php

1

Good morning. I'm trying to tinker with SOAP Web Service but I'm having problems. When I run the client.php file in the browser it does not work. Does anyone know how to solve it?

server.php

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

    
asked by anonymous 09.12.2017 / 15:37

2 answers

1

A ; is missing at the end of:

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

Should be:

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

Soon this error may be occurring:

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

However error exposure should be displayed on your site, so you will only receive the message 500 Internal Error Server .

Also note that your "maybe" server does not have the Soap extension for PHP enabled on php.ini and that possibly errors are turned off on your server you may not notice any 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 servidor.php on line 17

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

Enable SOAP extension in PHP

To do this you must have access to php.ini of your server, usually via CPanel or SSH, if you do not have access it 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 within php.ini the following line:

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

    ;extension=soap.so
    
  • If you are a Windows Server server

    ;extension=php_soap.dll
    

Then remove the ; from the front thus leaving:

extension=soap.so

Or if it's WindowsServer, like this:

extension=php_soap.dll

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

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

  

Welcome Douglas!
8

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

    
09.12.2017 / 16:18
0

If someone has a similar problem, try Guilherme's answer above, if he does not touch the php.ini as he said (as in my case was already right in php.ini), remove all lines from the server. php after the ?> . I mean, all the blank lines, for some reason that influences.

    
09.12.2017 / 21:26