DOMDocument和XPath,没有传递URL

时间:2014-01-28 13:21:13

标签: php xpath domdocument

我有这段代码,我首先要检查页面上是否存在对象,如果有,则应该选择该对象详细页面的URL。然后在该页面上,我想检查某些部分,无论它们是否存在,然后根据该部分返回TRUE或FALSE。

当我输入形成链接以检查数据所需的详细信息时,对于现有的细节,它不会为对象本身返回TRUE,更不用说该对象的部分了。

if ($_POST)
{
    $boolean_funda = FALSE;
    $boolean_jaap = FALSE;
    $aProducts = array();

    $plaats = $_POST['city'];
    $straat = $_POST['street'];
    $huisnummer = $_POST['housenumber'];

    # funda

    $plaats     = Funda_Objectinfo::toAscii($plaats);
    echo $plaats;
    $straat     = Funda_Objectinfo::toAscii($straat);
    echo $straat;
    $huisnummer = Funda_Objectinfo::toAscii($huisnummer);
    echo $huisnummer;

    $funda_url = 'http://www.funda.nl/koop/'.$plaats.'/straat-'.$straat.'/nr-'.$huisnummer.'/';

    $response = Funda_Objectinfo::sendRequest($funda_url);

    $object_street = Funda_Objectinfo::fetch_object_street($response);

    if ($object_street != FALSE)
    {
        $boolean_funda = TRUE;

        $funda_url = 'http://www.funda.nl'.$object_street;
        var_dump($funda_url);

        $response = Funda_Objectinfo::sendRequest($funda_url);
        var_dump($response);

        $response = Funda_Objectinfo::fetch_products($response, $object_street);

        if ($response != FALSE)
        {
            $aProducts['funda'] = $response;
        }
    }

}

以上是程序代码

以下是我使用的课程

class Funda_Objectinfo
{
    public static function toAscii($str, $replace = array(), $delimiter = '-')
    {
        if ( ! empty($replace))
        {
            $str = str_replace((array)$replace, ' ', $str);
        }

        $clean = preg_replace(array('/Ä/', '/Ö/', '/Ü/', '/ä/', '/ö/', '/ü/'), array('Ae', 'Oe', 'Ue', 'ae', 'oe', 'ue'), $str);
        $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean);
        $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
        $clean = strtolower(trim($clean, '-'));
        $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

        return $clean;
    }

    /*
     * @param String $funda_url
     * @return Mixed $html
     */
    public static function sendRequest($funda_url)
    {           
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $funda_url);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $html = curl_exec($ch);

        curl_close($ch);

        return $html;
    }       

    /*
     * @param String $html
     * @return Mixed $object_url
     */
    public static function fetch_object_street($html)
    {
        $dom = new DOMDocument();
        @$dom->loadHTML($html);

        $xpath = new DOMXPath($dom);

        $no_results = $xpath->query("//div[@class='no-results']");

        if ($no_results->length != 0)
        {
            return FALSE;
        }

        $elements = $xpath->query("//a[@class='object-street']/@href");

        $object_url = FALSE;

        foreach ($elements as $element)
        {
            $object_url = $element->nodeValue;
            break;
        }

        return $object_url;
    }

    /*
     * @param String $html
     * @param String $object_street
     * @return Mixed FALSE | Array
     */
    public static function fetch_products($html, $object_street)
    {
        $dom = new DOMDocument();
        @$dom->loadHTML($html);

        $xpath = new DOMXPath($dom);

        $no_results = $xpath->query("//div[@class='no-results']");

        if ($no_results->length != 0)
        {
            return FALSE;
        }

        $product_photo     = $xpath->query("//a[contains(@href,'{$object_street}fotos/')]");
        $product_360       = $xpath->query("//a[contains(@href,'{$object_street}360-fotos/')]");
        $product_blueprint = $xpath->query("//a[contains(@href,'{$object_street}plattegrond/')]");
        $product_video     = $xpath->query("//a[contains(@href,'{$object_street}video/')]");

        return array(
            'product_photo' =>     ($product_photo->length != 0 ? TRUE : FALSE),
            'product_360' =>       ($product_360->length != 0 ? TRUE : FALSE),
            'product_blueprint' => ($product_blueprint->length != 0 ? TRUE : FALSE),
            'product_video' =>     ($product_video->length != 0 ? TRUE : FALSE)
        );
    }

}

编辑:更多代码

<strong>Funda</strong>
<table border=1 width="300px">
    <tr>
        <td>Vermelding op Funda?</td>
        <td style="background-color: <?= ($boolean_funda == TRUE ? 'green' : 'red') ?>; width: 100px;"></td>
    </tr>
    <tr>
        <td>fotografie</td>
        <td style="background-color: <?= $aProducts['funda']['product_photo'] == 1 ? 'green' : 'red' ?>; width: 100px;"></td>
    </tr>
    <tr>
        <td>360 graden fotografie</td>
        <td style="background-color: <?= $aProducts['funda']['product_360'] == 1 ? 'green' : 'red' ?>; width: 100px;"></td>
    </tr>
    <tr>
        <td>plattegrond</td>
        <td style="background-color: <?= $aProducts['funda']['product_blueprint'] == 1 ? 'green' : 'red' ?>; width: 100px;"></td>
    </tr>
    <tr>
        <td>video</td>
        <td style="background-color: <?= $aProducts['funda']['product_video'] == 1 ? 'green' : 'red' ?>; width: 100px;"></td>
    </tr>
</table>



<form method="POST" action="objectinfo.php">
    <div>
        <label>Address:</label>
        <input type="text" name="street" value="<?= (isset($_POST['street']) ? $_POST['street'] : NULL) ?>" />
    </div>

    <div>
        <label>Housenumber:</label>
        <input type="text" name="housenumber" value="<?= (isset($_POST['housenumber']) ? $_POST['housenumber'] : NULL) ?>" />
    </div>

    <div>
        <label>City:</label>
        <input type="text" name="city" value="<?= (isset($_POST['city']) ? $_POST['city'] : NULL) ?>" />
    </div>

    <div style="margin-top: 10px"></div>

    <input type="submit" name="submit" value="Send Request" />
</form>

0 个答案:

没有答案