Created
January 21, 2009 09:11
-
-
Save mgng/49899 to your computer and use it in GitHub Desktop.
簡易HTTP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Http class | |
// 日本語UTF-8 | |
class Http | |
{ | |
public static function get($url, $header = null) { | |
$param = array( | |
'http'=>array('method'=>'GET') | |
); | |
if ($header !== null) { | |
$param['http']['header'] = self::_createHeader($header); | |
} | |
return self::_run($url, $param); | |
} | |
public static function post($url, $data, $header = null) { | |
$param = array( | |
'http' => array( | |
'method' => 'POST', | |
'header' => 'Content-type: application/x-www-form-urlencoded', | |
'content'=> $data | |
) | |
); | |
if($header !== null){ | |
$param['http']['header'] = $param['http']['header']."\n".self::_createHeader($header); | |
} | |
return self::_run($url, $param); | |
} | |
private static function _createHeader($header) { | |
if (!is_array($header)){ | |
return ''; | |
} | |
$r = array(); | |
foreach($header as $k=>$v){ | |
$r[]="{$k}: {$v}"; | |
} | |
return implode("\n", $r); | |
} | |
private static function _run($url, $param) { | |
$f = @fopen($url, 'rb', false, stream_context_create($param)); | |
if ($f === false){ | |
return false; | |
} | |
$r = @stream_get_contents($f); | |
if ($r === false){ | |
return false; | |
} | |
return $r; | |
} | |
} | |
$url = 'http://example.com/'; | |
$header = array( | |
'User-Agent' => 'Opera/9.25 (Windows NT 6.0; U; ja)', | |
'Referer' => 'http://example.com/' | |
); | |
$src = Http::get($url, $header); | |
echo $src; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment