0

I developed a web service using nusoap, seems that the web service works fine, in fact is very simple, I put here the code:

<?php
// Pull in the NuSOAP
require_once('lib/nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
//(MyService is name of our service)
$server->configureWSDL('WebService', 'urn:WebService');
// Character encoding
$server->soap_defencoding = 'utf-8';

//Register Insert user function
$server->register(
                  'InsertData',   //Name of function
                  array('Id' => 'xsd:int',
                        'userName' => 'xsd:string',
                        'Pass' => 'xsd:string',
                        'Mail' => 'xsd:string'), //Insert Values
                  array('return' =>'xsd:boolean'), //return Values
                  'urn:ServiceWsdl',  //Namespace
                  'urn:ServiceWsdl#InsertData',  //SoapAction
                  'rpc',       //style
                  'literal',   //can be encoded but it doesn't work with silverlight
                  'Insert function to register users'
                  );

//Register GetData function
$server->register(
                  'GetData',
                  array('Id' => 'xsd:int'),
                  array('Id' => 'xsd:int',
                        'userName' => 'xsd:string',
                        'Pass' => 'xsd:string',
                        'Mail' => 'xsd:string'), //return values
                  'urn:ServiceWsdl',
                  'urn:ServiceWsdl#GetData',
                  'rpc',
                  'literal',
                  'Get all users function'
                  );

function InsertData($id, $userName, $Pass, $Mail) {
    $connect = mysql_connect("server","userDB","passDB");
    if ($connect) {
        if(mysql_select_db("database", $connect)) {
            mysql_query( "INSERT INTO `Users`(`Id`, `UserName`, `Pass`, `Mail`) VALUES (`".$id."`,`".$userName."`,`".$Pass."´,`".$Mail."`);");
            return true;
        }
    }
    return false;
}

function GetData($Id) {
   $connect = mysql_connect("server","userDB","passDB");
        if ($connect) {
            if(mysql_select_db("database", $connect)) {
                $sql = "SELECT * FROM Users";
                $result = mysql_fetch_array(mysql_query($sql));
                    return $result['Id']."-".$result['UserName']."-".$result['Pass']."-".$result['Mail'];
             }
        }
            return false;
    }

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);


?>

And my code on the Android Xamarin project (working from Visual Studio 2013) is very simple too:

 protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = (Button) FindViewById<Button>(Resource.Id.MyButton);
        TextView labelText = (TextView)FindViewById<TextView>(Resource.Id.editTextUserName);
        button.Click += delegate
        {
            //showMessageWithName(labelText.Text);
            AvitecWS service = new AvitecWS();

            if (service.InsertData(69, "AnUser", "anUserPassword", "anUser@mail.com"))
            {
                //the following method just show a message :)
                showMessageWithName("Message has been send!");
            }
            else
            {
                showMessageWithName("Upss... something was wrong :(");
            }
        };

    }

Then, when I click the button and the app should to do the insert I 've got the following exception: enter image description here

I think that it is happening because the format of the SOAP object is not correct, but I can't view where is error :(

I REALLY appreciate any help.

Thanks in advance.

MadDev
  • 113
  • 16
  • Any particular reason you're using SOAP? It's miserable to deal with and I would recommend a simple REST service using JSON instead. – Jason Feb 16 '16 at 18:12
  • Mmm.... like for example? Can you recommend me a library? – MadDev Feb 16 '16 at 18:17
  • Ok... thats's true... I see the light! It's burnning me!!! ;) But... just for curiosity, I get a lot of documentation about nusoap and, well... seems thats my code is ok :( BTW, Somebody knows a good tuto about REST using PHP? – MadDev Feb 16 '16 at 18:28
  • Google "php rest framework" returns several interesting results. – Jason Feb 16 '16 at 20:09
  • Nice contribution... ;) Finally I achived that it works fine. I publish my code as an answer for an easy reading an increase the stackoverflow knowledge database. ;) – MadDev Feb 17 '16 at 21:08

1 Answers1

0

This exception throws when the client side cannot establish a connection with the server. In this case (I'm using Hostinger) I changed the numeric server IP for mysql.hostinger.com, the translated domain name, in fact is the same... but for a reason that I don't know it's just works using the translated domain name. My code:

 <?php
// Pull in the NuSOAP
require_once('lib/nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
//(MyService is name of our service)
$server->configureWSDL('AWebService', 'urn:AWebService');
// Character encoding
$server->soap_defencoding = 'utf-8';

//Register Insert user function
$server->register(
                  'InsertData',   //Name of function
                  array('userName' => 'xsd:string',
                        'Pass' => 'xsd:string',
                        'Mail' => 'xsd:string'), //Insert Values
                  array('return' =>'xsd:boolean'), //return Values
                  'urn:AwebServiceWsdl',  //Namespace
                  'urn:AwebServiceWsdl#InsertData',  //SoapAction
                  'rpc',       //style
                  'literal',   //can be encoded but it doesn't work with silverlight
                  'Insert function to register users'
                  );

   function InsertData($userName, $Pass, $Mail) {
    $connect = mysqli_connect("mysql.hostinger.es", [USER], [PASSWORD], [DATABASE]);
    if ($connect) {

        mysqli_query($connect, "INSERT INTO `Users`(`UserName`, `Pass`, `Mail`) VALUES ('".$userName."','".$Pass."','".$Mail."')");
                     return true;
                     }
                     return false;
                     }


$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);


?>

By the way, I changed the mysql_connect() method to mysqli_connect() method, because the first is deprecated. Another mysql methods are changed too. More info here:

Deprecated: mysql_connect()

Community
  • 1
  • 1
MadDev
  • 113
  • 16