Webservice Php without Lib

1

Oops, I took a look at the Stack behind a simple question, but it's making me confused. Knowing the concept of Webservice is relatively easy, but practice is the focus of this question. I saw some other questions here, but that were not answered because of how the question was put, so I want to specify, exemplify the best possible so that they can help me.

It's easy to find Libs to develop a Webservice, the question is, can I conceptually create a Webservice without any Lib? For example, receiving the data via Post, or Get, manipulates it and return a response in Json, or XML for an application developed on a different platform? Keeping interoperability and other features of a webservice ...

As Gambiarra has done this:

<?php
    include 'servidor.php';

    $resposta=array();

    if(isset($_GET['comando'])){
        $comando=$_GET['comando'];
        switch($comando){
            case "Inserir":
                if(isset($_GET['nome']) && isset($_GET['ra'])){
                    $r=inserirContato($_GET['nome'] , $_GET['ra']);
                        if($r){
                            $resposta['codigo']=1;
                            $resposta['mensagem']="Sucesso ao inserir!";
                        }else{
                            $resposta['codigo']=0;
                            $resposta['mensagem']="Erro ao inserir!";
                        }
                }else{
                    $resposta['codigo']=0;
                    $resposta['mensagem']="Campos ausentes!";
                }
        break;

But I've already done this:

<?php
    include 'conexao.class.php';
    include 'lib/nusoap.php'; // BAIXADO DA INTERNET

    $server = new nusoap_server;
    $server->configureWSDL('agenda','urn:agenda');
    $server->wsdl->schemaTargetNamespace='urn:agenda';


    $server->wsdl->addComplexType(
        'Contato',
        'complexType',
        'struct',
        'all',
        '',
        array(
            'id'=>array('name'=>'id','type'=>'xsd:int'),
            'nome'=>array('name'=>'nome','type'=>'xsd:string'),
            'telefone'=>
            array('name'=>'telefone','type'=>'xsd:string')
        )
    );

    $server->wsdl->addComplexType(
        'ArrayString',
        'complexType',
        'array',
        '',
        'SOAP-ENC:Array',
        array(),
        array(
            array('ref'=>'SOAP-ENC:arrayType',
            'wsdl:arrayType'=>'xsd:string[]')
        ),
        'xsd:string'
    );

    $server->register(
        'procurarContato',
        array('id'=>'xsd:int'),
        array('return'=>'tns:Contato'),
        'urn:agenda',
        'urn:agenda#procurarContato',
        'rpc',
        'encoded',
        'Procurar contato'
    );

    function procurarContato($id){
        $con = new conexao();
        if($con->connect()){
            $query=mysql_query("SELECT * FROM contato where id='$id'");
            $row = mysql_fetch_array($query);
            $contatos=array(
                'id'=>$row['id'],
                'nome'=>$row['nome'],
                'telefone'=>$row['telefone']
            );
            $con->disconnect(); 
            return $contatos;
        }else return null;      
    }

Regarding the delivery of the desired information both worked, although one I used a lib to have documentation and be something more formal and the other not. It may seem that my question is the answer itself, but then there is my doubt, these two forms above are Webservices? If not, how would you classify the first form? I can improve the issue if it seems too broad or too vague. (Remembering that the excerpts from the codes pasted here are just to illustrate the use of the library, there are still functions, parses for responses etc.

    
asked by anonymous 26.10.2016 / 01:37

1 answer

1

Roughly, both can be considered webservices yes.

What changes between them is the pattern to use.

In the first case there is no default, only a confirmation message and nothing else. Error handling is not intuitive, among other things.

In the second case, you are using a pattern called SOAP. This pattern uses a specific XML format to define the type of expected request and which response should be returned, depending on your system's implementation. What changes is that error handling, the format of the responses follow a standard specification.

How will you implement that which is the point. You can try to create a webservice for communication between SOAP systems without any lib, but it will not be very productive how to use a ready lib. Following a specification is not trivial.

When providing a webservice, think not of your implementation, but of how the person you are going to consume will work. By using an already familiar pattern, the person consuming the service will focus on what it needs and not on deciphering its magic API.

What do you prefer as a user, consuming the service of example 1 or the service of example 2?

    
26.10.2016 / 01:55