function do_get($url) { $res = get_contents($url); $res_array = explode("\r\n", $res); switch ($res_array[0]) { // とりあえず必要なコードのみ。 case 'HTTP/1.0 200 OK': case 'HTTP/1.1 200 OK': // ボディを取り出す。 $ra = explode("\r\n\r\n", $res, 2); $res = $ra[1]; break; case 'HTTP/1.1 302 MovedTemporarily': // 移動先URLを取り出す。 foreach ($res_array as $res_row) { if (strstr($res_row, 'Location: ')) { $crr_url = str_replace('Location: ', '', $res_row); break; } } $res = get_contents($crr_url); // ボディを取り出す。 $ra = explode("\r\n\r\n", $res, 2); $res = $ra[1]; break; default: $res = ''; } return $res; } function get_contents($url) { // ホストとポートを取得する。 $url_array = parse_url($url); $host = $url_array['host']; $path = $url_array['path']; if (array_key_exists('port', $url_array)) { $port = $url_array['port']; } else { switch ($url_array['scheme']) { case 'http': $port = 80; break; } } $query = $url_array['query']; $res = false; if (isset($host) && isset($port)) { $fp = fsockopen($host, $port, $errno, $errstr, 30); if ($fp) { $req = "GET " . $path . "?" . $query . " HTTP/1.0\r\n" . "Host: " . $host . "\r\n" . "\r\n"; socket_set_timeout($fp, 10); if (fputs($fp, $req, strlen($req))) { $res = ''; while (!feof($fp)) { $res .= fgets($fp); } } fclose($fp); } } return $res; }http://saikyoline.jp/weblog/2007/02/file_get_contents_1.html
1 / 2| 次のページ »
[コメントが多かった順の記事]・人の心理の裏をかくホームページ集客術:リピータになってもらうためには(233)
・ホームページ心理学第2弾「メールの書きだしとホームページ運営・集客の関係」(76)
・娘が生まれた(69)
・ゲームと言えば、何を思い出す? (50)
・Microsoftからアクセス (30)
・「目標の立て方」と「努力」と「実行力」(26)
最近のコメント