Jump to content

[GUIDE]How to connect to SOAP using PHP

Guest chmuun

Recommended Posts

Hey guys,

I've been getting a lot of help from the community so I thought why don't i help you guys too as a return.

I've made a function for my custom website but I've changed this function to work normally without any libraries

Just make sure you have PHP 5.0.1 or above

$settings = array(
   "IP"        => "", //Your server's IP address
   "PORT"        => 7878, //7878 is the default Port
   "USERNAME"    => "USERNAME", //Must be in UPPERCASE in both, Database AND This file
   "PASSWORD"    => "Password", //Password of the account give above
   "DEBUG"        => true, //If "True" then errors will be printed in html and if false ... They'll only be printed in file
function ExecuteSoapCommand($command)
   global $settings;
   //Setup SOAP Client
   $client = new SoapClient(NULL,
       "location" => "http://".$settings['IP'].":".$settings['PORT']."/",
       "uri" => "urn:MaNGOS",
       "style" => SOAP_RPC,
       "login" => $settings['USERNAME'],
       "password" => $settings['PASSWORD'],

   try //Try to execute function
       $result = $client->executeCommand(new SoapParam($command, "command"));
   catch(Exception $e) //Don't give fatal error if there is a problem
       return array('sent'=>false, 'message'=>$e->getMessage());
   return array('sent'=>true, 'message'=>$result);
function LogSoapError($e)
   global $settings;
   $date = date('D d/m/Y');
   $time = date('G:i:s');
   $ip = $_SERVER['REMOTE_ADDR'];
   $error = $e->getMessage();
   $errorcode = $e->getCode();
   $file = $e->getFile();
   $line = $e->getLine();

$errorstring = "\\r\\n
|----------------------------SOAP Command Error-----------------------------------
|Date: $date, Time: $time, From: $ip
|Where: $file(Line: $line) Error Code: $errorcode
|Error: $error
|----------------------------SOAP Command Error-----------------------------------";

   $f = fopen("soaperror.log", "a+");
   fwrite($f, $errorstring);

   //IF $debug == true the error will be shown in HTML
       print $errorstring;

I haven't tested the code yet because my server is down because VRTServers sucks really hard.

Never get a server from them, lame customer support.

Make sure you edit the $settings and the "DEBUG" is for showing the error and what kinda error it is.. in full details

Here is an example of how to use this script

$soap_command = ExecuteSoapCommand("server info");
   print $soap_command['message']; //This is tell you the server info
   print "There was an error while executing SOAP command. Please contact an administrator.";

Errors will be logged in file "soaperror.log"

If you have an problems just reply

Have Fun

Link to comment
Share on other sites

Only thing I can comment on is that you should have two functions handeling SOAP

ExecuteSoapCommand() and also ConnectToSoap() or something similar, your current ExecuteSoapCommand() function creates a new SOAP connection everytime it is called, however this is not needed and is wasting resources.

Instead ConnectToSoap() would connect to SOAP one time and then ExecuteSoapCommand() should execute the commands only on the connected socket resource. This makes sense if you use ExecuteSoapCommand 2 or more times on the same PHP script.

Think about MySQL + PHP, if you can have 1 query to do a task as opposed to 3 queries, which one makes more sense in terms of efficiency. Otherwise nice contribution, I really like the logging/debug portion

Link to comment
Share on other sites

A tip for future reference:

   $f = fopen("soaperror.log", "a+");
   fwrite($f, $errorstring);

This can be rewritten in a single statement using the file_put_contents method:

   file_put_contents('soaperror.log', $errorstring, FILE_APPEND);

However, limit your file-writes to a minimum. I strongly suggest disabling file-logging as well with debug off, imagine a bigger script with a dozen methods like this, even a simple pageview would mean a dozen (slow!) filewrites - you don't want that. You're better off building a logging method (or using a framework with one) that writes out all log entries at the end of a script in one call.

Also, be careful with underscores in front of your function names. A double underscore is used for magic methods (like __get(), __construct(), etcetera), using a single underscore is a bug waiting to happen.

Last but not least, why put the try / catch inside the function? What you're doing now is catching an error if it occurs, and then manually add an error to your output - so why do this at all? Just let the exception propogate and let calling methods deal with it. That way you can always simply return the SOAPresult string without having to worry about boolean return types :)

Overall though there is already a good example of how to use procedural PHP code to deal with the SOAP interface in the source (/contrib/soap/example.php). If you want to add to that, an object oriented example would be a lot more useful in my opinion.

Link to comment
Share on other sites


Thanks for the tips but

Only errors are logged and if the command it correct... there will be no logs... Logs are really important... maybe some one is using a variable in a function... He won't even know if there are errors

try and catch is added because Soap commands creates a FATAL error if there is even one problem and on development sites it is better to give a custom error message than a auto php error message with all command info and connection info


I've fixed the few errors and i've also removed the _ from the function... i only added it for my php class

Link to comment
Share on other sites

  • 1 month later...
  • 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. Privacy Policy Terms of Use