0

I have a Wordpress app that consumes an external API and connects to it via NuSoap Client request. I first developed this in localhost WAMP server, PHP 7.4, and got it working, but now I've migrated the project into a live server and it the same request doesn't work now.

This is my code PHP Nusoap Client code:

<?php
require_once(dirname(__FILE__) . '/nusoap.php');
ini_set('display_errors','On');

$userID = $_POST['UserId'];
$unitID = $_POST['UnitId'];
$serviceID = $_POST['ServiceId'];
$onlineUserID = $_POST['IdUserOnline'];

$soapClient = new nusoap_client('http://example-request-url.com?wsdl', 'wsdl');
var_dump($soapclient);//returns NULL

$error = $soapClient->getError();

if ($error) {
 die("client construction error: {$error}\n");
}    

$soapClient->soap_defencoding = 'UTF-8';
$soapClient->decode_utf8 = FALSE;

try{
   $params = array(
       'UserId' => $userID,
       'UnitId' => $unitID,
       'ServiceId' => $serviceID,
       'IdUserOnline' => $onlineUserID
   );
   //print_r($params);
   // array('parameters' => $params)
   $response = $soapClient->call('CreateSlip', array('parameters' => $params), '', '', false, true);  

   if ($soapClient->fault) {
       echo 'Error: ';
       print_r($response);
   }
   else{
       $error = $soapClient->getError();

       if ($error) {
           die("client construction error: {$error}\n"); //returns "client construction error: wsdl error: Getting http://example-request-url.com?wsdl 
                                                         //- HTTP ERROR: Couldn't open socket connection to server http://example-request-url.com?wsdl, Error (111): Connection refused
       }  
   }
   // $response = $soapClient->__soapCall('CreateSlip', $params);
   var_dump($response);//returns "bool(false)"
   $json_response = json_encode($response);
   echo $json_response;
   print_r(json_last_error_msg());
} catch (SoapFault $e){
   echo "Error: {$e}";//does not echo
}
?>

My live server has PHP 8.1, thought that might have been the issue but after changing the version to match the one from my WAMP server it still didn't work. The response I get from the API is false, meaning that the request was not correct. As I've mentioned before, on WAMP locahost this worked perfectly, any ideas on what might be wrong with my code?

Thanks in advance.

  • Which of your echo commands produces the `false` output, specifically? – ADyson Jan 06 '23 at 13:26
  • @ADyson the $json_response. I've edited my post with the results of each echo in front to better illustrate – Ricardo Carneiro Jan 06 '23 at 13:53
  • Ok, so, `json_encode` can return `false` when it fails (as per https://www.php.net/manual/en/function.json-encode.php). Have you tried `var_dump($response);` to see what the original response object looks like? It's possible it simply isn't encodable for some reason. [json_last_error_msg](https://www.php.net/manual/en/function.json-last-error-msg.php) can also help you to find out why a JSON operation failed. – ADyson Jan 06 '23 at 14:00
  • @ADyson it returns bool(false) – Ricardo Carneiro Jan 06 '23 at 14:03
  • Ok. So the call seems to be failing entirely. Have you considered the possibility that the hosting environment doesn't allow outbound HTTP connections? There could be a firewall blocking it. You should ask the system administrator / hosting provider for details on that. – ADyson Jan 06 '23 at 14:11
  • You can also try to see if the nusoap library reports any faults, e.g. as per the suggestions here: https://stackoverflow.com/a/30021991/5947043 – ADyson Jan 06 '23 at 14:12
  • @ADyson ok good sugestion, it now displays the following error: "message client construction error: wsdl error: Getting http://example-request-url.com?wsdl - HTTP ERROR: Couldn't open socket connection to server http://example-request-url.com?wsdl, Error (111): Connection refused". I've update my code with these changes. – Ricardo Carneiro Jan 06 '23 at 14:21
  • Thanks. That strongly suggests a firewall issue – ADyson Jan 06 '23 at 14:47
  • @ADyson what would be the solution here then? should I contact my host provider or is it a problem on my own machine? what would you suggest? – Ricardo Carneiro Jan 06 '23 at 15:08
  • Well the request is going from your hosted server to the remote URL. So clearly it never comes near your machine. Like you said yourself, it worked fine when you ran it locally on WAMP. I mentioned earlier that you should contact the hosting company. – ADyson Jan 06 '23 at 15:22
  • 1
    @ADyson thank you very much for all the help. It was indeed an issue for my hosting company, the request sent to the port I was sending, were being bloqued, I've spoken to them and now alll is working as it should, thanks once again – Ricardo Carneiro Jan 09 '23 at 09:01

1 Answers1

1

UPDATE on this.

My issue was being caused because the PORT which the requests were being sent was closed. After exposing this to my hosting company they fixed it right away.