0

我正在慢慢处理一个加载类的表单,并且需要通过该类运行表单字段以使用W3C 的 HTML Validator's API验证代码。表单字段将包含 url。我还没有开始验证输入的数据。我只是想获得相同的页面来返回数据。在如何在表单操作中执行 PHP 函数的帮助下,我获得了 form.php 代码?现在是:

<?php
require_once ( 'api_w3cvalidation.class.php' );
$validate = new W3cValidateApi;
if (isset($_POST)) {
  $uri = $_POST['uri']; // this will get you what was in the
                                    // textfield if the form was submitted
                                    $a = $validate->validate($uri);
                                    if($a){
                                        echo 'Verified!';
                                    } else {
                                        echo 'Not verified!<br>';
                                        echo 'Errors found: ' . $validate->ValidErrors;
                                    }
};

?>

<form name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']) ;?>" >
  <p>Your uri is: <?php echo $uri;?></p>
    <label>
      <input type="text" name="uri" id="textfield">
    </label>
    <label>
      <input type="submit" name="button" id="button" value="Submit">
    </label>
</form>

我正在使用的课程是:

<?
/*
   Author:  Jamie Telin (jamie.telin@gmail.com), currently at employed Zebramedia.se

   Scriptname: W3C Validation Api v1.0 (W3C Markup Validation Service)

   Use:     
        //Create new object
            $validate = new W3cValidateApi;

            //Example 1
                $validate->setUri('http://google.com/');    //Set URL to check
                echo $validate->makeValidationCall();       //Will return SOAP 1.2 response

            //Example 2
                $a = $validate->validate('http://google.com/');
                if($a){
                    echo 'Verified!';
                } else {
                    echo 'Not verified!<br>';
                    echo 'Errors found: ' . $validate->ValidErrors;
                }

            //Example 3
                $validate->ui_validate('http://google.com/'); //Visual display

            //Settings
                $validate->Output       //Set the type of output you want, default = soap12 or web
                $validate->Uri          //Set url to be checked
                $validate->setUri($uri) //Set url to be checked and make callUrl, deafault way to set URL
                $validate->SilentUi     //Set to false to prevent echo the vidual display
                $validate->Sleep        //Default sleeptime is 1 sec after API call
*/

class W3cValidateApi{

    var $BaseUrl = 'http://validator.w3.org/check';
    var $Output = 'soap12';
    var $Uri = '';
    var $Feedback;
    var $CallUrl = '';
    var $ValidResult = false;
    var $ValidErrors = 0;
    var $Sleep = 1;
    var $SilentUi = false;
    var $Ui = '';

    function W3cValidateApi(){
        //Nothing...
    }

    function makeCallUrl(){
        $this->CallUrl = $this->BaseUrl . "?output=" . $this->Output . "&uri=" . $this->Uri;
    }

    function setUri($uri){
        $this->Uri = $uri;
        $this->makeCallUrl();
    }

    function makeValidationCall(){
        if($this->CallUrl != '' && $this->Uri != '' && $this->Output != ''){
            $handle = fopen($this->CallUrl, "rb");
            $contents = '';
            while (!feof($handle)) {
                $contents .= fread($handle, 8192);
            }
            fclose($handle);
            $this->Feedback = $contents;
            sleep($this->Sleep);
            return $contents;
        } else {
            return false;
        }
    }

    function validate($uri){
        if($uri != ''){
            $this->setUri($uri);
        } else {
            $this->makeCallUrl();
        }

        $this->makeValidationCall();

        $a = strpos($this->Feedback, '<m:validity>', 0)+12;
        $b = strpos($this->Feedback, '</m:validity>', $a);
        $result = substr($this->Feedback, $a, $b-$a);
        if($result == 'true'){
            $result = true;
        } else {
            $result = false;
        }
        $this->ValidResult = $result;

        if($result){
            return $result;
        } else {
            //<m:errorcount>3</m:errorcount>
            $a = strpos($this->Feedback, '<m:errorcount>', $a)+14;
            $b = strpos($this->Feedback, '</m:errorcount>', $a);
            $errors = substr($this->Feedback, $a, $b-$a);
            $this->ValidErrors = $errors;
        }
    }

    function ui_validate($uri){
        $this->validate($uri);

        if($this->ValidResult){
            $msg1 = 'This document was successfully checked';
            $color1 = '#00CC00';
        } else {
            $msg1 = 'Errors found while checking this document';
            $color1 = '#FF3300';
        }
        $ui = '<div style="background:#FFFFFF; border:1px solid #CCCCCC; padding:2px;">
                    <h1 style="color:#FFFFFF; border-bottom:1px solid #CCCCCC; margin:0; padding:5px; background:'.$color1.'; font-family:Arial, Helvetica, sans-serif; font-size:16px; font-weight:bold;">
                     '.$msg1.'
                    </h1>
                    <div>
                        <strong>Errors:</strong><br>
                        '.$this->ValidErrors.'
                    </div>
                </div>';
        $this->Ui = $ui;
        if($this->SilentUi == false){
            echo $ui;
        }
        return $ui;

    }

}
?>

我需要做什么才能使添加到文本字段的 url 通过类并在提交 url 后显示是否已验证?

更新

进行了一些更改,验证似乎有效。只有它仍然显示在实际验证之前未验证的站点。

4

1 回答 1

1

您不小心测试了是否设置了 POST 超全局。

<?php
/* ... */
if (isset($_POST)) {
  /* ... */
}

该变量几乎总是被设置。相反,请尝试测试其中是否有任何数据。

<?php
if (count($_POST)) {
  /* ... */
}

如果您想真正准确地测试此页面加载是否是 POST 的结果(例如,即使没有发布任何值,您仍然希望以不同的方式处理请求),您可以直接检查请求方法。

<?php
if ($_SERVER['REQUEST_METHOD'] === "POST") {
  /* ... */
}
于 2012-05-09T10:53:04.443 回答