21 DE May DE 2021
Para este post vamos a usar Twingly como ejemplo, que es un servicio que funciona para hacer ping para notificar que el contenido de nuestro blog fue actualizado, usa el protocolo XML-RPC y el formato de documentos con XML para procesar la información.
Laravel tiene un wrapper HTTP para hacer peticiones que funciona desde la versión 7 del framework.
El cliente HTTP implementa (entre otras cosas), un método llamado send
que posee la siguiente firma:
1send(string $method, string $url, array $options = [])
Entonces, para hacer el ping a twingly solo debemos hacer lo siguiente:
1$xml = ' 2<?xml version="1.0"?> 3<methodCall> 4 <methodName>weblogUpdates.ping</methodName> 5 <params> 6 <param> 7 <value>'. config('app.name') .'</value> 8 </param> 9 <param>10 <value>'. config('app.url') .'</value>11 </param>12 </params>13</methodCall>';14 15Http::withHeaders([16 'Content-Type' => 'text/xml; charset=utf-8'17])->send('POST', 'https://rpc.twingly.com/', [18 'body' => $xml,19]);
Si la petición fue correcta vamos a obtener una respuesta de este tipo:
1<?xml version="1.0" ?> 2<methodResponse> 3 <params> 4 <param> 5 <value> 6 <struct> 7 <member> 8 <name>flerror</name> 9 <value>10 <boolean>0</boolean>11 </value>12 </member>13 <member>14 <name>message</name>15 <value>16 <string>Thanks for the ping.</string>17 </value>18 </member>19 </struct>20 </value>21 </param>22 </params>23</methodResponse>
Los invito a leer la documentación completa de twingly.
Made with ♥️ & 🧉 in Montevideo, Uruguay.
[AC].dev is not affiliated with Laravel or Laravel LLC. Laravel is a Trademark of Taylor Otwell.
Code highlighting provided by Torchlight.
We use cookies!
Hi, this website uses essential cookies to ensure its proper operation and tracking cookies to understand how you interact with it.