Aprender cURL [parte 2/2]
Antes de leer esta entrada os recomiendo daros un paseo por la primera parte, en la que os argumento el porqué de cURL además de unos ejemplos para empezar. Bien, continuaremos con un par de ejemplos algo más complejos, vamos a usar cURL para buscar palabras en el diccionario:
$ curl dict://dict.org/d:stalwart
220 miranda.org dictd 1.9.15/rf on Linux 2.6.26-bpo.1-686
<400549.18119.1238445667@miranda.org>
250 ok
150 1 definitions retrieved
151 “Stalwart” gcide “The Collaborative International Dictionary of English v.0.48″
Stalwart \Stal”wart\ (st[o^]l”w[~e]rt or st[add]l”-; 277),
Stalworth \Stal”worth\ (-w[~e]rth), a. [OE. stalworth, AS.
staelwyr[eth] serviceable, probably originally, good at
stealing, or worth stealing or taking, and afterwards
extended to other causes of estimation. See {Steal}, v. t.,
{Worth}, a.]
Brave; bold; strong; redoubted; daring; vehement; violent. “A
stalwart tiller of the soil.” –Prof. Wilson.
[1913 Webster]Fair man he was and wise, stalworth and bold. –R. of
Brunne.
[1913 Webster]Note: Stalworth is now disused, or but little used, stalwart
having taken its place.
[1913 Webster]
.
250 ok [d/m/c = 1/0/20; 0.000r 0.000u 0.000s]
221 bye [d/m/c = 0/0/0; 0.000r 0.000u 0.000s]
Con lo que podemos reemplazar la palabra ‘stalwart‘ por la que deseemos definir. Además del uso bajo línea de comandos, todas las capacidades de cURL están disponibles en la librería libcurl. Muchos lenguajes de programación incluyen una interfaz de conexión con cURL (libcurl) para automatizar tareas tales como la transmisión de un archivo a través de FTP. Por ejemplo, este fragmento de código PHP usa libcurl para colgar un archivo cargado por un formulario en un servidor FTP:
<?php
…
$ch = curl_init();
$localfile = $_FILES[‘upload’][‘tmp_name’];
$fp = fopen($localfile, ‘r’);
curl_setopt($ch, CURLOPT_URL,
‘ftp://ftp_login:password@ftp.domain.com/’.$_FILES[‘upload’][‘name’]);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
…
?>
¿Alguna otra utilidad? ¿Le das otro uso a cURL?