简体   繁体   中英

PHP Webservice using nuSoap and mySQL

I'm currently developing a PHP webservice that will be consumed by the Document Management Software DocuWare . Right now it mostly consists of reading the database and getting a value by their ID. The folder for the webservice consists of the following folders:

dataLayer

ServiceLayer

frontEndLayer

lib (nusoap)

samples (nusoap)

In dataLayer I have client.php

<?php
    require "../lib/nusoap.php";
        $url="http://localhost/service/dataLayer/service.php";
        $client=new nusoap_client($url."?wsdl",'wsdl');
        $code=$_POST[idCourse];
        $courses=$client->call('listCourses',array("code"=>$code),'uri:'.$url,'uri:'.$url.'/listCourses');
        if($client->fault){
        echo "Error";
        print_r($courses);
        }else{
            if($client->getError()){
                echo '<b>Error: '.$client->getError().'</b>';
            }else{
                print_r($courses);
            }
        }
?>

and connection.php

<?php
    require(../dataLayer/auth.php);
    function connect(){
        $conn = mysql_connect($host,$hostuser,$hostpw)or die(mysql_error());
        mysql_select_db("tickets",$conn);
        return($conn);
    }
?>

With that in mind; I made the serviceLayer which consists of a simple "service.php" file.

<?php
    include('../lib/nusoap.php');
    require ('../dataLayer/connection.php');
    $url = "http://localhost/service/dataLayer/service.php";
    $server->configureWSDL("consult",$url);
    $server->wsdl->schematargetNamespace=$url;
    $server->soap_defencoding='utf-8';
    $server->register;
             ("listcourses",
             array("code"=> "xsd:string"),
             array("return => "xsd:string"), $url
             );

    function listcourses($code){
        $conn=connect();
        if($code!=0){
            $sql="select id from ticket where id='$code'";
        }else{
            $sql="select id from ticket";
        }
        $rs=mysql_query($sql,$conn);
        $i=0;
        $chainset="<?xml version='1.0' encoding='utf-8'?>";
        if($rs!=null){
            $chainset.="<courses>";
            if(mysql_num_rows($rs)>0){
                while ($row = mysql_fetch_row($rs)){
                    $chainset.="<course>";
                    $chainset.="<br>";
                    $chainset.="<code>".row[0]."<code>";
                    $chainset.="<br>";
                    $chainset.="<name>".row[1]."<name>";
                    $chainset.="</course";
                    $i++;
                }
            }else{
                $chainset.="<error>No data available</error>"
            }
            $chainset.="</course";
        }else{
            $chainset.="<error>Error".mysql_error()."</error>";
        }
        $repl=new soapval('return','xsd:string',$chainset);
        return $repl;
    }
    if(!isset($HTTP_RAW_POST_DATA))
    $HTTP_RAW_POST_DATA=file_get_contents('php://input');
    $server->service($HTTP_RAW_POST_DATA);
?>

Theorically everything is OK . I made the front end layer, which is just a simple form.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <script src="jquery/jquery2.1.js"></script>
        <script>
            function sendForm(form){
                document.getElementById(form).submit();
            }
        </script>
    </head>
    <body>
        <div class="container">
            <section id="content">
                <form id="login" name="login" method="POST" action="../dataLayer/client.php">
                    <div>
                        <label for="idCourse">Course</label>
                    </div>
                    <br>
                    <div>
                        <input type="text" maxlength="2" id="idCourse" name="idCourse" placeholder="Example - 76" required="required" size="10"/>
                    </div>
                    <input type="reset" name="button" value="Reset"/>
                    <input type="submit" name="button" value="Submit"/>
                </form>
            </section>
        </div>
    </body>
</html>

And I get the following error:

(: ) Notice: Use of undefined constant idCourse- assumed 'idCourse' in C.\wamp64\www\service\dataLayer\client.php on line 5 Call Stack

Time Memory Function Location 1 0.0003 245144 {main}( )...\client.php:0 Error: wsdl error: XML error parsing WSDL from

http://localhost/service/dataLayer/service.php?wsdl on line 2:

Invalid document end

I don't know what's wrong since everything seems to be fine.

As pointed by Jaquarh , made the following correction:

In client.php:

$code=$_POST['idCourse']; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM