views:

38

answers:

2

I have a directory full of XML files. For each of these files I make a search at RIPE. For each search I do a couple of RegEx searches through the returned HTML code. But after a couple of loops, file_get_contents stop returning data, and all my operations after are done on an empty string.

I figured PHP may be timing out since these pages take a while to load. But wouldn't the script execution stop completely then? Instead all the loops finish and output their HTML code, though without content.

I'm also guessing there could be some sort of maximum requests pr second deal with PHP.

Could anyone here shed some light on this?

Thanks


Edit: To explain my title, a friend of mine and myself were running the script at the same time. That's why I'm guessing PHP sets a limit to how many requests it can send pr minute or something, because it seems PHP manages a varying number of loops before it stops returning data.


Edit: Added some code: (I figured it wasn't needed, due to my explanation of the problem)

<?php
set_time_limit(0);

include "pagebase.php";

$page = new pagebase();
$page->jQuery = true;
$page->formatDoc = false;
$page->addScript("javascript.js");
$page->addStylesheet("../codeclean.css");
$page->addStylesheet("stylesheet.css");
$page->title = "...";

$directory_path = "xml_documents";

$directory = scandir($directory_path);
$files = array();

foreach($directory as $string)
{
    if(preg_match("/.*\.xml/", $string, $result) > 0)
        array_push($files, $result[0]);
}

$content =
    "
        <table cellpadding=\"0\" cellspacing=\"0\">
            <tr>
                <td colspan=\"7\">
                    <center><h2>...</h2></center>
                </td>
            </tr>
            <tr>
                <td class=\"header_cell\">Case ID</td>
                <td class=\"header_cell\">Description (From RIPE)</td>
                <td class=\"header_cell\">IP</td>
                <td class=\"header_cell\">Fil</td>
                <td class=\"header_cell\">Time</td>
                <td class=\"header_cell\">Type</td>
            </tr>
    ";

foreach($files as $index => $file)
{
    $xml = simplexml_load_file("$directory_path/$file");
    $id = trim($xml->Case->ID);
    $ip = trim($xml->Source->IP_Address);
    $title = trim($xml->Content->Item->Title);
    $time = trim($xml->Source->TimeStamp);
    $type = trim($xml->Source->Type);

    $desc_result = array();
    $info_result = array();

    $RIPE_result = file_get_contents("http://www.db.ripe.net/whois?searchtext=$ip");
    preg_match("/(?<=descr:)(\s*)(.*)/", $RIPE_result, $desc_result);
    preg_match_all("/<pre>.*<\/pre>/sm", $RIPE_result, $info_result);

    $info_result[0] = implode("", $info_result[0]);

    if(count($desc_result) < 1) $desc_result[0] = "<font style=\"color:red\">No description found</font>";
    else $desc_result[0] = trim($desc_result[0]);

    $content .=
        "
            <tr id=\"info_row_$index\">
                <td class=\"info_cell\">$id</td>
                <td class=\"info_cell\">$desc_result[0]</td>
                <td class=\"info_cell\">$ip</td>
                <td class=\"info_cell\">$title</td>
                <td class=\"info_cell\">$time</td>
                <td class=\"info_cell\">$type</td>
            </tr>
            <tr id=\"expanded_row_$index\">
                <td class=\"expanded_cell\" colspan=\"7\">
                    <div id=\"content_container_$index\">
                        <input type=\"button\" class=\"pastey_button\" rel=\"$index\" value=\"Get info\" />
<div id=\"RIPE_$index\">$info_result[0]</div>
                    </div>
                </td>
            </tr>
        ";
}

$content .=
    "
            <tr>
                <td colspan=\"6\">Vi har totalt ".count($files)." henvendelser.</td>
            </tr>
        </table>
    ";

$page->body = $content;
$page->drawPage();
?>

Testing inline code blocks

A: 

If by timing out you mean file_get_contents timing out I'm pretty sure that'll throw an error (or at least return false). As far as I know PHP doesn't have a number of HTTP requests it can fulfil per execution.

How many items are you talking about here? Have you checked the values for those items?

You could try using set_time_limit(0) but PHP should throw an error if PHP's reaching maximum execution time so you might not need that.

Ross
No errors are thrown. I'm testing my script with 10 XML files, and it gets between 1 and 4 items down the list before it stops returning information.set_time_limit(0) had no effect by the way
Codemonkey
A: 

i think RIPE has usage limits - you may be being locked out if you perform too many queries in a certain amount of time.

PeterJ
That's one of my other worries. Hopefully someone here will be able to tell for certain.
Codemonkey
Unfortunately, after further testing it turned out this was the case. Thanks for your input
Codemonkey