oriolrius.cat

Des del 2000 compartiendo sobre…

Tag: php-class

HttpClient – PHP Web Client Class

Reading time: 1 – 2 minutes

Aquesta classe ens permet implementar de forma senzillissima un client HTTP 1.0 i 1.1 tan per peticions GET com POST. De fet, fa algo semblant al que que comentava de la classe de perl WWW:Mechanize tot i que amb molt menys detalls. Malgrat això pot ser molt útil per interactuar amb altres webs des del nostre codi en PHP. A més és senzillissim usar aquesta classe. HttpClient implementa les següent funcionalitats.

  • Implements a useful subset of the HTTP 1.0 and 1.1 protocols.
  • Includes cookie support.
  • Ability to set the user agent and referal fields.
  • Can automatically handle redirected pages.
  • Can be used for multiple requests, with any cookies sent by the server resent for each additional request.
  • Support for gzip encoded content, which can dramatically reduce the amount of bandwidth used in a transaction.
  • Object oriented, with static methods providing a useful shortcut for simple requests.
  • The ability to only read the page headers – useful for implementing tools such as link checkers.
  • Support for file uploads.

Per més informació recomano que mireu els exemples que hi ha a la web de la classe és realment simple d’usar.

phpMailer – una classe de PHP per enviar emails

Reading time: 5 – 8 minutes

En PHP quan s’ha d’enviar un email sovint s’usa la funció mail() tot i que aquesta és una mica limitada en quan a paràmetres que se li poden passar al servidor SMTP. Per exemple, temes d’autenticació i xifrat. Via pear hi ha diverses solucions però a vegades cal que la solució que incorpori el nostre aplicatiu sigui independent d’aquest tipus de requisits de sistema. Així doncs phpMailer ens pot servir per sortir de més d’un embolic.

Suporta:

  • Can send emails with multiple TOs, CCs, BCCs and REPLY-TOs
  • Redundant SMTP servers
  • Multipart/alternative emails for mail clients that do not read HTML email
  • Support for 8bit, base64, binary, and quoted-printable encoding
  • Uses the same methods as the very popular AspEmail active server (COM) component
  • SMTP authentication
  • Word wrap
  • Address reset functions
  • HTML email
  • Tested on multiple SMTP servers: Sendmail, qmail, Postfix, Imail, Exchange, etc
  • Works on any platform
  • Flexible debugging
  • Custom mail headers
  • Multiple fs, string, and binary attachments (those from database, string, etc)
  • Embedded image support

Un petit exemple d’ús perquè vegeu el senzill que és usar-lo:

require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();                                   // send via SMTP
$mail->Host     = "smtp1.site.com;smtp2.site.com"; // SMTP servers
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "jswan";  // SMTP username
$mail->Password = "secret"; // SMTP password
$mail->From     = "from@email.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@site.com","Josh Adams");
$mail->AddAddress("ellen@site.com");               // optional name
$mail->AddReplyTo("info@site.com","Information");
$mail->WordWrap = 50;                              // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz");      // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
$mail->IsHTML(true);                               // send as HTML
$mail->Subject  =  "Here is the subject";
$mail->Body     =  "This is the <b>HTML body</b>";
$mail->AltBody  =  "This is the text-only body";
if(!$mail->Send())
{
   echo "Message was not sent <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
echo "Message has been sent";

Aquesta troballa l’he fet a través de l’article phpMailer , a great Email transfer class del blog de Moody Bahrain.