cakephp HttpResponseがクラス名かぶってる

概要

  • cakephp 2.2でhttpSocket使ってスクレイピングしようとしたけど、戻り値のHttpResponseにあるべきプロパティやメソッドがない

サンプルコード

    public function index(){
        // disable view
        $this->autoRender = false;

        App::uses('HttpSocket', 'Network/Http');
        $http = new HttpSocket();
        $url = "http://cakephp.jp/";
        $response = $http->get( $url );
        if( ! $response ){
            return false;
        }

        echo $response->body();
    }

画面表示

Fatal error: Call to undefined method HttpResponse::body() in /home/atsushi-kambayashi/www/app/Controller/HogeController.php on line 29

メソッド一覧確認

下記コードでメソッド一覧確認

    public function index(){
        // disable view
        $this->autoRender = false;

        App::uses('HttpSocket', 'Network/Http');
        $http = new HttpSocket();
        $url = "http://cakephp.jp/";
        $response = $http->get( $url );
        if( ! $response ){
            return false;
        }

        echo "<pre>";
        var_dump( get_class_methods( $response ) );
        echo "</pre>";

        echo $response->body();
    }

出力はこんな感じ

array(34) {
  [0]=>
  string(9) "setHeader"
  [1]=>
  string(9) "getHeader"
  [2]=>
  string(7) "setETag"
  [3]=>
  string(7) "getETag"
  [4]=>
  string(15) "setLastModified"
  [5]=>
  string(15) "getLastModified"
  [6]=>
  string(21) "setContentDisposition"
  [7]=>
  string(21) "getContentDisposition"
  [8]=>
  string(14) "setContentType"
  [9]=>
  string(14) "getContentType"
  [10]=>
  string(16) "guessContentType"
  [11]=>
  string(8) "setCache"
  [12]=>
  string(8) "getCache"
  [13]=>
  string(15) "setCacheControl"
  [14]=>
  string(15) "getCacheControl"
  [15]=>
  string(7) "setGzip"
  [16]=>
  string(7) "getGzip"
  [17]=>
  string(16) "setThrottleDelay"
  [18]=>
  string(16) "getThrottleDelay"
  [19]=>
  string(13) "setBufferSize"
  [20]=>
  string(13) "getBufferSize"
  [21]=>
  string(7) "setData"
  [22]=>
  string(7) "getData"
  [23]=>
  string(7) "setFile"
  [24]=>
  string(7) "getFile"
  [25]=>
  string(9) "setStream"
  [26]=>
  string(9) "getStream"
  [27]=>
  string(4) "send"
  [28]=>
  string(7) "capture"
  [29]=>
  string(8) "redirect"
  [30]=>
  string(6) "status"
  [31]=>
  string(17) "getRequestHeaders"
  [32]=>
  string(14) "getRequestBody"
  [33]=>
  string(20) "getRequestBodyStream"
}

cakephpのバグか

クラス名変えて確認

$ ls -la lib/Cake/Network/Http/
合計 72
drwxr-xr-x 2 atsushi-kambayashi atsushi-kambayashi  4096 2012-09-09 18:13 .
drwxr-xr-x 4 atsushi-kambayashi atsushi-kambayashi  4096 2012-09-01 10:28 ..
-rw-r--r-- 1 atsushi-kambayashi atsushi-kambayashi  1754 2012-09-01 10:28 BasicAuthentication.php
-rw-r--r-- 1 atsushi-kambayashi atsushi-kambayashi  3262 2012-09-01 10:28 DigestAuthentication.php
-rw-r--r-- 1 atsushi-kambayashi atsushi-kambayashi 10564 2012-09-09 17:51 HttpResponse.php
-rw-r--r-- 1 atsushi-kambayashi atsushi-kambayashi 29381 2012-09-09 18:13 HttpSocket.php

HttpSocketに使用するレスポンスクラスの名称があるので変更

/**
 * Response classname
 *
 * @var string
 */
    public $responseClass = 'CakeHttpResponse'; // 元はHttpResponse

同ディレクトリにクラスコピーして内部のクラス名を変更

$ cp lib/Cake/Network/Http/HttpResponse.php lib/Cake/Network/Http/CakeHttpResponse.php 

/**
 * HTTP Response from HttpSocket.
 * 
 * @package       Cake.Network.Http
 */
class CakeHttpResponse implements ArrayAccess { 

メソッド名出力外して確認

とりあえずページ情報はとれた
f:id:kambayashi:20120909183032p:plain