Socket通信
開発環境:CakePHP2.5.1
CakePHPでsocket通信を行うにはHttpSocketクラスを利用します。
getリクエスト
getリクエストを行う場合には下記のようにします。
1 2 3 4 5 6 7 |
<?php public function index() { App::uses('HttpSocket', 'Network/Http'); $HttpSocket = new HttpSocket(); $results = $HttpSocket->get('http://www.google.com/search', array('q' => 'cakephp')); } ?> |
postリクエスト
postリクエストを行う場合には下記のようにします。
1 2 3 4 5 6 7 |
<?php public function index() { App::uses('HttpSocket', 'Network/Http'); $HttpSocket = new HttpSocket(); $results = $HttpSocket->post('http://www.google.com/search', array('q' => 'cakephp')); } ?> |
※上記の例はサンプルで、googleの検索でpostリクエストを受け付けていないので、405エラーになります。
柴田 篤志
最新記事 by 柴田 篤志 (全て見る)
- WordPressとCakePHPの共存 - 2014年10月22日
- サイトマップの作成方法 - 2014年10月12日
- INSERTしたIDを取得する - 2014年10月4日