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

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

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

全記事へのリンク:

#10 レスポンスコードをチェックする

php:
  1. <?php
  2. /**
  3.  * 例 48-10 レスポンスコードをチェックする
  4.  *
  5.  * @link http://pear.php.net/manual/ja/package.http.http-request.response-eval.php
  6.  */
  7. require_once 'HTTP/Request2.php';
  8.  
  9. $urls = array(
  10.     "http://www.example.com/",
  11.     "http://example.com/thisdoesnotexist.html"
  12.     );
  13.  
  14. try {
  15.     $req = new HTTP_Request2();
  16.     foreach ($urls as $url) {
  17.         $req->setUrl($url);
  18.         $response = $req->send();
  19.  
  20.         $code = $response->getStatus();
  21.         switch ($code) {
  22.             case 404:
  23.                 echo "Document not foundn";
  24.                 break;
  25.  
  26.             case 200:
  27.                 echo "Everything's okn";
  28.                 break;
  29.         }
  30.     }
  31.  
  32. } catch (HTTP_Request2_Exception $e) {
  33.     die($e->getMessage());
  34. } catch (Exception $e) {
  35.     die($e->getMessage());
  36. }
  37.  
  38. /*****************************
  39.  * HTTP_Request::getResponseCode()はHTTP_Request2_Response::getStatus()に変更されました。
  40.  *****************************/
  41.  
  42. /*
  43. require_once "HTTP/Request.php";
  44.  
  45. $urls = array(
  46.     "http://www.example.com/",
  47.     "http://example.com/thisdoesnotexist.html"
  48.     );
  49.  
  50. $req =& new HTTP_Request("");
  51. foreach ($urls as $url) {
  52.     $req->setURL($url);
  53.     $req->sendRequest();
  54.  
  55.     $code = $req->getResponseCode();
  56.     switch ($code) {
  57.     case 404:
  58.         echo "Document not foundn";
  59.         break;
  60.  
  61.     case 200:
  62.         echo "Everything's okn";
  63.         break;
  64.  
  65.     }
  66. }
  67.  
  68. */
  69. ?>

#11 レスポンスからの全てのヘッダを取得する

php:
  1. <?php
  2. /**
  3.  * 例 48-11 レスポンスからの全てのヘッダを取得する
  4.  *
  5.  * @link http://pear.php.net/manual/ja/package.http.http-request.response-eval.php
  6.  */
  7. require_once 'HTTP/Request2.php';
  8.  
  9. try {
  10.     $req = new HTTP_Request2("http://example.com/");
  11.     $response = $req->send();
  12.     foreach ($response->getHeader() as $name => $value) {
  13.         echo $name . " = " . $value . "n";
  14.     }
  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::getResponseHeader()はHTTP_Request2_Response::getHeader()に変更されました。
  24.  *****************************/
  25.  
  26. /*
  27. require_once "HTTP/Request.php";
  28.  
  29. $req =& new HTTP_Request("http://example.com/");
  30. $req->sendRequest();
  31.  
  32. foreach ($req->getResponseHeader() as $name => $value) {
  33.     echo $name . " = " . $value . "n";
  34. }
  35.  
  36. */
  37. ?>

#12 特定のヘッダを取得する

php:
  1. <?php
  2. /**
  3.  * 例 48-12 特定のヘッダを取得する
  4.  *
  5.  * @link http://pear.php.net/manual/ja/package.http.http-request.response-eval.php
  6.  */
  7. require_once 'HTTP/Request2.php';
  8.  
  9. try {
  10.     $req = new HTTP_Request2("http://example.com/");
  11.     $response = $req->send();
  12.     echo $response->getHeader('Date');
  13.  
  14. } catch (HTTP_Request2_Exception $e) {
  15.     die($e->getMessage());
  16. } catch (Exception $e) {
  17.     die($e->getMessage());
  18. }
  19.  
  20. /*****************************
  21.  * HTTP_Request::getResponseHeader()はHTTP_Request2_Response::getHeader()に変更されました。
  22.  *****************************/
  23.  
  24. /*
  25. require_once "HTTP/Request.php";
  26.  
  27. $req =& new HTTP_Request("http://example.com/");
  28. $req->sendRequest();
  29.  
  30. echo $req->getResponseHeader("Date");
  31.  
  32.  
  33. */
  34. ?>


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

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

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

コメントする

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