Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Barcamp

1,442 views

Published on

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

Published in: Education
  • Be the first to comment

  • Be the first to like this

Barcamp

  1. 1. XML-RPC <ul><ul><li>Como crear un API para tu sitio usando </li></ul></ul><ul><ul><li>XML-RPC y PHP </li></ul></ul><ul><ul><li>Barcamp 2 - Queretaro </li></ul></ul><ul><ul><li>René López Caballero </li></ul></ul>
  2. 2. ¿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
  3. 3. ¿Quienes lo usan? <ul><li>Flickr puede usar REST, SOAP, XML-RPC </li></ul><ul><li>Wordpress </li></ul><ul><li>Movable Type </li></ul><ul><li>Weblogs.Com </li></ul>
  4. 4. 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>
  5. 5. Tipos de Datos
  6. 6. 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>
  7. 7. 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>
  8. 8. Libreria http://phpxmlrpc.sourceforge.net/
  9. 9. 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; } }
  10. 10. 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;), ) ); ?>
  11. 11. 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 ); ?>

×