CakePHPの「HttpSocketクラス」なるものがHTTP通信を簡単に実現してくれます
// sockets_controller.php <?php class SocketsController extends AppController { var $name = 'Sockets'; var $uses = null; function get_result() { uses('http_socket'); $socket = new HttpSocket(); $url = 'http://www.google.co.jp/search'; $query = array('q' => $this->params['url']['query']); $result = $socket->get($url, $query); //$this->set('result', $result); echo $result; } }
これでgetリクエスト(get_result?query=〜)で受け取った値で
そのままGoogle検索の結果を表示することができました。
ソケットはFTP、POP、SMTPといった他プロトコルを扱いたい場合でも
「CakeSocketクラス」っていう低レベル通信を行うクラスがあるので安心(?)
っていうか、先方のサーバで動かすプログラムからCakeのクラスとか使えないじゃん・・・