Tim Levinsson
Tim Levinsson

Reputation: 597

php soap redirect after execution not working

I am running a private server for a game and i have an issue with soap. If the server is online everything works perfectly fine and it can redirect me after running soap command but if my server is closed then it wont redirect me anymore. Instead it will show me this error

Fatal error: Uncaught SoapFault exception: [HTTP] Could not connect to host in C:\xampp\htdocs\SoapSite\testfile.php:16 Stack trace: #0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'http://127.0.0....', 'urn:TC#executeC...', 1, 0) #1 C:\xampp\htdocs\SoapSite\testfile.php(16): SoapClient->__call('executeCommand', Array) #2 {main} thrown in C:\xampp\htdocs\SoapSite\testfile.php on line 16

So here is my code

<?php
$soapHost = "127.0.0.1";
$soapPort = "7878";
$soapUsername = "soapuser";
$soapPassword = "12345";

$client = new SoapClient(NULL, array(
    'location' => "http://$soapHost:$soapPort/",
    'uri'      => 'urn:TC',
    'style'    => SOAP_RPC,
    'login'    => $soapUsername,
    'password' => $soapPassword,
));

$command = "account create ".$_POST['uname']." ".$_POST['pw'];
$result = $client->executeCommand(new SoapParam($command, 'command'));
header('location: ../index.php');
?>

Upvotes: 0

Views: 209

Answers (1)

ArSeN
ArSeN

Reputation: 5248

Well, since the fatal error occurs because the exception is not caught, the PHP script is stopped at that point and sending your redirect (Location Header) is not executed anymore.

I suggest to catch the exception:

// [...] other code before the execute here
try {
  $result = $client->executeCommand(new SoapParam($command, 'command'));
}
catch (SoapFault $e) {
  // could add some optional logging/error handling here
}
header('location: ../index.php');

Alternatively you could somehow ping the server beforehand and only run the request if it is actually there, but that might be cumbersome depending on response times and timeout settings and such.

Upvotes: 1

Related Questions