Uploaded on

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

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

More in: Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,159
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
6
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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