CakePHPããJSONãã¤ããã¢ãã«ãã¤ãã£ã
JSONã§ã·ãªã¢ã©ã¤ãºããã¦ããAPIãããããããªã¢ãã«ãä½ã£ã¦ã¿ã¾ããããã¼ã«ã«ã«é
ç½®ãã¦ããJSONã®ãã¡ã¤ã«ã«ã対å¿ãã¦ãã¾ãã
ãã£ãã·ã¥ãããããããªä»çµã¿ã«ãªã£ã¦ããã®ã§ãå¤æ°å©ããããªå ´åã«ãå¹æçã§ãã
modelã®ä¸ã«ãjson.phpã¨ãããã¡ã¤ã«åã§ä¿åãã¦ãã ããã
<?php class Json extends AppModel { var $name = 'Json'; var $useTable = false ; /* * jsonãã¼ã¿ã®åå¾ * @param string JSONãã¡ã¤ã«ã¸ã®ãã¹ * @param boolean ãã£ã·ã¥ãããã * @return mixed */ function find( $path , $cache = false ){ if ( !substr_count( $path , 'http://' ) && !file_exists( $path ) ){ return false ; } if ( $cache ){ $cache_key = md5( $path ); if (($data=Cache::read($cache_key))!==false){ return $data ; } $data = call_user_func_array(array($this,__FUNCTION__),array($path,false)); Cache::write( $cache_key , $data ); } $data = json_decode(file_get_contents($path),true); return $data ; } /* * jsonã®ãã£ãã·ã¥ãã¯ãªã¢ * @param string jsonãã¡ã¤ã«ã¸ã®ãã¹ * @return boolean */ function cache_clear( $path = null ){ return Cache::delete( md5($path) ); } }
使ãããã¨ãã¯ç°¡åã§ãã以ä¸ã®ããã«ã³ã³ããã¼ã©ã¼ããå¼ã³åºãã°OKã§ããã¨ããããTwitterAPIã§ãµã³ãã«ãã
<?php class AppController extends Controller { var $uses = array('Json'); function get(){ $data = $this->Json->find('http://api.twitter.com/1/statuses/public_timeline.json'); debug( $data ); } }
ããã§JSONã®åãæ±ãããã楽ã«ãªãã¾ããã¼ï¼