[php] web情報の取得を3つの手段で行う
WEBverのPHP Cookからの情報。 Web情報の取得情報における3つの方法についてのってたので転載。file_get_contensが無いのは仕様? あの関数はやっぱあんまり使わないほうがいいのかな。
その1
$page = ''; $fh = fopen('http://www.example.com/robots.txt','r') or die($php_errormsg); while (! feof($fh)) { $page .= fread($fh,1048576); } fclose($fh);
You can use the cURL extension:
$c = curl_init('http://www.example.com/robots.txt'); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); $page = curl_exec($c); curl_close($c);
You can also use the HTTP_Request class from PEAR:
require 'HTTP/Request.php'; $r = new HTTP_Request('http://www.example.com/robots.txt'); $r->sendRequest(); $page = $r->getResponseBody();
curlのやり方が気に入った。思っていたより短くてすむ。