[php][pear]HTTP_Request2のサンプル#2

カテゴリ: php / author: uechoco / 2009年01月21日 23:30:06
この記事を読む時間:15くらい

この記事は、HTTP_Requestのマニュアルに載っている全サンプルコードをHTTP_Request2対応で書き換えてみる企画の2回目です。HTTP_Request2クラスの概要を知りたい方は、下記リンクの#1を参照ください。 全記事へのリンク:

#3 Basic 認証

php:
  1. <?php
  2. /**
  3.  * 例 48-3 Basic 認証
  4.  *
  5.  * @link http://pear.php.net/manual/ja/package.http.http-request.basic-auth.php
  6.  */
  7. require_once 'HTTP/Request2.php';
  8.  
  9. try {
  10.     $req = new HTTP_Request2("http://example.com/protected.html");
  11.     $req->setAuth("johndoe", "foo", HTTP_Request2::AUTH_BASIC);
  12.     $response = $req->send();
  13.     echo $response->getBody();
  14.  
  15. } catch (HTTP_Request2_Exception $e) {
  16.     die($e->getMessage());
  17. } catch (Exception $e) {
  18.     die($e->getMessage());
  19. }
  20.  
  21. /*****************************
  22.  * HTTP_Request::setBasicAuth()はHTTP_Request2->setAuth()に変更されました。
  23.  * 第3引数で認証の種類(AUTH_BASIC、AUTH_DIGEST)が指定できますが、
  24.  * Basic認証(AUTH_BASIC)がデフォルト値なので、今回は省略してもかまいません。
  25.  *****************************/
  26.  
  27. /*
  28. require_once "HTTP/Request.php";
  29.  
  30. $req =& new HTTP_Request("http://example.com/protected.html");
  31. $req->setBasicAuth("johndoe", "foo");
  32.  
  33. $response = $req->sendRequest();
  34.  
  35. if (PEAR::isError($response)) {
  36.     echo $response->getMessage();
  37. } else {
  38.     echo $req->getResponseBody();
  39. }
  40.  
  41. */
  42. ?>

#4 リクエストにクッキーを追加する

php:
  1. <?php
  2. /**
  3.  * 例 48-4 リクエストにクッキーを追加する
  4.  *
  5.  * この例では、version というクッキーが HTTP リクエストに追加されます。
  6.  * このクッキーの値は、 HTTP_Request2 インスタンスが動作している PHP インタプリタのバージョン文字列です。
  7.  *
  8.  * @link http://pear.php.net/manual/ja/package.http.http-request.cookie.php
  9.  */
  10. require_once 'HTTP/Request2.php';
  11.  
  12. try {
  13.     $req = new HTTP_Request2("http://example.com/");
  14.     $req->addCookie("version", phpversion());
  15.     $response = $req->send();
  16.     echo $response->getBody();
  17.  
  18. } catch (HTTP_Request2_Exception $e) {
  19.     die($e->getMessage());
  20. } catch (Exception $e) {
  21.     die($e->getMessage());
  22. }
  23.  
  24. /*****************************
  25.  * HTTP_Request::addCookie()はHTTP_Request2->addCookie()としてそのまま受け継がれています。
  26.  *****************************/
  27.  
  28. /*
  29. require_once "HTTP/Request.php";
  30.  
  31. $req =& new HTTP_Request("http://example.com/");
  32. $req->addCookie("version", phpversion());
  33.  
  34. $response = $req->sendRequest();
  35.  
  36. if (PEAR::isError($response)) {
  37.     echo $response->getMessage();
  38. } else {
  39.     echo $req->getResponseBody();
  40. }
  41. */
  42. ?>

#5 HTTP レスポンスからクッキーを読み込む

php:
  1. <?php
  2. /**
  3.  * 例 48-5 HTTP レスポンスからクッキーを読み込む
  4.  *
  5.  * HTTP レスポンスのクッキーを読み込みは、以下の例で示されます。
  6.  *
  7.  * @link http://pear.php.net/manual/ja/package.http.http-request.cookie.php
  8.  */
  9. require_once 'HTTP/Request2.php';
  10.  
  11. try {
  12.     $req = new HTTP_Request2("http://example.com/");
  13.     $response = $req->send();
  14.     print_r($response->getCookies());
  15.  
  16. } catch (HTTP_Request2_Exception $e) {
  17.     die($e->getMessage());
  18. } catch (Exception $e) {
  19.     die($e->getMessage());
  20. }
  21.  
  22. /*****************************
  23.  * HTTP_Request::getResponseCookies()はHTTP_Request2_Response->getCookies()に変更されました。
  24.  *****************************/
  25.  
  26. /*
  27. require_once "HTTP/Request.php";
  28.  
  29. $req =& new HTTP_Request("http://example.com/");
  30.  
  31. $response = $req->sendRequest();
  32.  
  33. if (PEAR::isError($response)) {
  34.     echo $response->getMessage();
  35. } else {
  36.     print_r($req->getResponseCookies());
  37. }
  38. */
  39. ?>


コメントはまだありません »

コメントはまだありません。

この投稿へのコメントの RSS フィード。 TrackBack URI

コメントする

Copyright © 2012 うえちょこ@ぼろぐ. WP Theme created by Web Top.