DOMDocument and XPath, no URL passed

2019-09-03 03:25发布

I have this piece of code, I first want to check if an object exists on the page, if it does, it should select the URL to the detailed page of that object. Then on that page, I want to check for certain parts, whether they exist or not and then return TRUE or FALSE per part, based on that existence.

When I enter the details needed to form a link to check the data from, with existing details, it does not return TRUE for the object itself, let alone the parts of that object.

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;
        }
    }

}

Above is the procedural code

Below is the class I use

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)
        );
    }

}

EDIT: More code

<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条回答
登录 后发表回答