Webservice Php without Lib

Asked

Viewed 85 times

1

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

It is easy to find Libs to develop a Webservice, the question is, conceptually can I create a Webservice without any Lib? For example, receive the data via Post, or Get, manipulate it and return a Json response, or XML to an application developed on a different platform? Maintaining interoperability and other features of a web service...

How Gambiarra ever did that:

<?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 done that too:

<?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 a documentation and be something more formal and the other not. It may seem that my question is the answer of itself, but there is my doubt, these two forms above are Webservices? If not, how would you classify the first form? I can improve the question if it seems too broad or vague. (Remembering that the snippets of the codes pasted here are only to illustrate the use of the library, there are still the functions, the parses for the repeats etc.

  • 1

    conceitualmente posso criar um Webservice sem nenhuma Lib? Conceptually, Libs are "standard" webservices, created with no lib, so you don’t have to create your own :)

  • Ricardo ball show, thank you very much!

1 answer

1


Roughly, both can be considered webservices yes.

What changes between them is the pattern to be used.

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

The second case is using a pattern called SOAP. This standard uses a specific XML format to define the expected request type and which response should be returned, according to your system’s implementation. What changes is that the handling of errors, the format of the responses follow a standard specification.

How will you implement that which is the point. You can try to create a web service for communication between SOAP systems without any lib, but it won’t be as productive as using a ready lib. Following a whole specification is not something trivial.

When providing a webservice, think not about your implementation, but about how the person who will consume will work. By using a known pattern, the person consuming the service will focus on what they need and not on deciphering their magic API.

What you prefer as user, consume example 1 service or example 2 service?

  • I understood perfectly, with certainty example 2 because it would have a documentation and a more consistent standard to follow. Thank you very much, brother!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.