PHPã§file_get_contents使ã£ãHTTPæ¥ç¶ãã¨ã©ã¼ãªã®ãã¿ã¤ã ã¢ã¦ããªã®ããå¤å¥ãã
PHPã§ãµã¼ã-ãµã¼ãéã§REST-APIã使ãã¨ããfile_get_contentsã使ãã®ãç°¡åãªãã§ãããã¨ã©ã¼ã¬ã¹ãã³ã¹ã®å ´åããã®ç¶æ³ãã©ã解éãããããã®ãã¨ãã説æãã©ããããã¾ããªæ°ãããã®ã§ãã¡ãã£ã¨èª¿ã¹ã¦ã¿ã¾ããã
ã¬ã¹ãã³ã¹ã³ã¼ã200ã¨ã¨ã©ã¼ãããã¨ãã¿ã¤ã ã¢ã¦ããå¤å¥ããæ¹æ³ã¯ãã©ããããããªæãã¿ããã§ãã
<?php $data = @file_get_contents('http://hostname/resouce', false, stream_context_create(array( 'http' => array( 'timeout'=>10 // æ©ãã«ã¿ã¤ã ã¢ã¦ã ) )) ); if($data === FALSE) { if(count($http_response_header) > 0) { $stat_tokens = explode(' ', $http_response_header[0]); switch($stat_tokens[1]) { case 404: // 404 Not found ã®å ´å break; case 500: // 500 Internal Server Error ã®å ´å break; default: // ãã®ä» break; } } else { // ã¿ã¤ã ã¢ã¦ãã®å ´å } } else { // æå } ?>
file_get_contentsãHTTPãªã¯ã¨ã¹ãã ã£ãå ´åã$http_response_header ã¨ããé åã«ããã®çµæã®ã¬ã¹ãã³ã¹ããããå ¥ã£ã¦ããã®ã ã¨ããHTTPã¬ã¹ãã³ã¹ãããã®å é è¡ã¯ã¹ãã¼ã¿ã¹ãªã®ã§ãããããã¨ã©ã¼ã³ã¼ããåãã¾ããã
ã¿ã¤ã ã¢ã¦ãããå ´åã¯ãã®é åã空ã«ãªã£ã¦ãã¾ããããªã®ã§ãé·ãã¼ãã§åå²ãã¦ãã¾ãã
ã¨ãããããããªæãããªã®ããªã
PEAR::HTTP_Request使ãã°ãããããã¨ãã声ãèããããããªæ°ãããªãããªãã§ãããæ°ã®ããã§ãããã£ã¨ã