• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Barcamp
 

Barcamp

on

  • 2,356 views

Como generar un API para tu sitio web usando XML-RPC

Como generar un API para tu sitio web usando XML-RPC

Statistics

Views

Total Views
2,356
Views on SlideShare
2,221
Embed Views
135

Actions

Likes
0
Downloads
5
Comments
0

3 Embeds 135

http://www.rene.com.mx 125
http://10.0.5.13:8080 9
http://localhost:8080 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Barcamp Barcamp Presentation Transcript

    • XML-RPC
        • Como crear un API para tu sitio usando
        • XML-RPC y PHP
        • Barcamp 2 - Queretaro
        • René López Caballero
    • ¿XML-RPC? Es una especificacion que permite a software corriendo en diferentes SO y ambientes realizar llamadas a procedimientos sobre el internet Es una llamada a un procedimiento remoto usando HTTP y XML para la codificacion
    • ¿Quienes lo usan?
      • Flickr puede usar REST, SOAP, XML-RPC
      • Wordpress
      • Movable Type
      • Weblogs.Com
    • Llamada XML-RPC <?xml version=&quot;1.0&quot;?> <methodCall> <methodName>jibberjobber.getContacts</methodName> <params> <param> <value><struct> <member><name>name</name> <value><string>John</string></value> </member> <member><name>lname</name> <value><string>Doe</string></value> </member> </struct></value> </param> </params> </methodCall>
    • Tipos de Datos
    • Struct y Array <struct> <struct> <member> <name>name</name> <value><string>John</string></value> </member> <member> <name>age</name> <value><i4>21</i4></value> </member> </struct> <array> <array> <data> <value><i4>12</i4></value> <value><string>Egypt</string></value> <value><boolean>0</boolean></value> <value><int>-31</int></value> </data> </array>
    • Respuestas XML <?xml version=&quot;1.0&quot;?> <methodResponse> <params> <param> <value><string>South Dakota</string></value> </param> </params> </methodResponse> <?xml version=&quot;1.0&quot;?> <methodResponse> <fault> <value> <struct> <member> <name>faultCode</name> <value><int>4</int></value> </member> <member> <name>faultString</name> <value><string>Too many parameters.</string></value> </member> </struct> </value> </fault> </methodResponse>
    • Libreria http://phpxmlrpc.sourceforge.net/
    • Ejemplo Servidor <?php include( 'xmlrpc/xmlrpc.inc' ); include( 'xmlrpc/xmlrpcs.inc' ); $xmlrpc_defencoding = &quot;UTF8&quot;; $xmlrpc_internalencoding = 'UTF-8'; function getStructValue( &$struct, $key ) { if( $struct->structMemExists( $key ) ) { $val = $struct->structMem($key); return $val->scalarVal(); } else { return false; } }
    • Continua function add( $params ) { global $xmlrpcerruser; $struct = $params->getParam(0); $name = getStructValue( $struct, 'name' ); // Aqui se prueba si el usuario esta autorizado if( true ){ $struct = array( 'id' => new xmlrpcval( $id, 'int' ), 'dateCreated' => new xmlrpcval( $data['dateCreated'], 'dateTime.iso8601' ) ); return new xmlrpcresp( new xmlrpcval( $struct, 'struct' ) ); } else { return new xmlrpcresp( 0, $xmlrpcerruser,, &quot;The login failed&quot; ); } } $s = new xmlrpc_server( array( &quot;example.add&quot; => array(&quot;function&quot; => &quot;add&quot;), ) ); ?>
    • Ejemplo del cliente <?php include( 'xmlrpc/xmlrpc.inc' ); include( 'xmlrpc/xmlrpcs.inc' ); $client = new xmlrpc_client( &quot;http://127.0.0.1/barcamp/xml-rpc.php&quot; ); $client->setDebug( 2 ); $struct = array( 'username' => new xmlrpcval('demo', 'string'), 'password' => new xmlrpcval('demo', 'string' ), 'name' => new xmlrpcval('John', 'string' ) ); $message = new xmlrpcmsg( &quot;example.add &quot;, array( new xmlrpcval( $struct, 'struct' ) ) ); $response = $client->send( $message ); ?>