PHP PEAR HTTP相关Package介绍

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>';
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值