PHP PEAR HTTP相关Package
PEAR中的HTTP可以实现诸如远程页面抓取,判断页面的有效性,SOCKET操作,模拟身份验证,提交,上传,下载文件等相关的HTTP操作,在这零零碎碎的收集了一些代码,以备后用,没有整理。
HTTP_Request抓取远程页面
<?php
require_once "HTTP/Request.php";
//一个页面
$req =& new HTTP_Request("http://www.b2c-battery.co.uk/");
if (! PEAR::isError($req->sendRequest())) {
echo $req->getResponseBody();
}
//第二个页面
$req->setMethod(HTTP_REQUEST_METHOD_POST);//HTTP_REQUEST_METHOD_GET
$req->addPostData("Foo", "bar");
$req->setURL("http://pear.php.net");
$req->clearPostData();
if (!PEAR::isError($req->sendRequest())) {
$response2 = $req->getResponseBody();
} else {
$response2 = "";
}
echo $response2;
?>
//获得身份认证的页面(Authentication验证方式)
<?php
require_once "HTTP/Request.php";
$req =& new HTTP_Request("http://www.examples.com/protected.php");
$req->setBasicAuth("111", "111");
$response = $req->sendRequest();
if (PEAR::isError($response)) {
echo $response->getMessage();
} else {
echo $req->getResponseBody();
}
?>
//检测一个指定的URL是否可用
<?php
require_once "HTTP/Request.php";
$urls = array(
"http://www.example.com/",
"http://example.com/thisdoesnotexist.html"
);
$req =& new HTTP_Request("");
foreach ($urls as $url) {
$req->setURL($url);
$req->sendRequest();
$code = $req->getResponseCode();
switch ($code) {
case 404:
echo "Document not found/n";
break;
case 200:
echo "Everything's ok/n";
break;
/* ... */
}
}
?>
//下载
常用的文件类型有:
application/pdf
application/zip
application/x-octetstream//默认
text/css
image/jpeg
等等
<?php
require_once 'PEAR.php';
require_once 'HTTP/Download.php';
PEAR::setErrorHandling(PEAR_ERROR_PRINT);
//$params = @$_GET['params'];
$_GET['op'] = 'send';
$_GET['what'] = 'file';
switch ($_GET['what']) {
case 'file':
$params['file'] = 'phpbb.tar.gz';
break;
case 'resource':
$params['resource'] = fopen('data.txt', 'rb');
break;
case 'data':
$params['data'] = file_get_contents('data.txt');
break;
}
switch ($_GET['op']) {
case 'static':
HTTP_Download::staticSend($params);
break;
case 'send':
$h = &new HTTP_Download;
//以此设置下载的参数
//$params['contenttype'] = ...
//$h->setParams($params);
$h->send();
break;
case 'arch':
HTTP_Download::sendArchive('foo.'. $_GET['type'], $_GET['what'], $_GET['type']);
break;
}
?>
//文件上传
<html><body>
<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF'];?>?submit=1" method="post" enctype="multipart/form-data">
Send these files:<br>
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
<input name="userfile" type="file"> <-<br>
<input name="otherfile[]" type="file"><br>
<input name="otherfile[]" type="file"><br>
<input type="submit" value="Send files">
</form>
</body></html>
<?php
error_reporting(E_ALL);
if (!isset($submit)) {
exit;
}
require 'HTTP/Upload.php';
echo '<pre>';
print_r($_FILES);
$upload = new http_upload('en');
$file = $upload->getFiles('userfile');
if (PEAR::isError($file)) {
die ($file->getMessage());
}
if ($file->isValid()) {
$file->setName('uniq');//唯一的文件名
$dest_dir = './uploads/';//上传路径
$dest_name = $file->moveTo($dest_dir);//上传后的文件名
if (PEAR::isError($dest_name)) {
die ($dest_name->getMessage());
}
$real = $file->getProp('real');//原来的文件名
echo "Uploaded $real as $dest_name in $dest_dir/n";
} elseif ($file->isMissing()) {
echo "No file selected/n";
} elseif ($file->isError()) {
echo $file->errorMsg() . "/n";
}
print_r($file->getProp());
echo '</pre>';
?>