SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quick PHP question;

    I'm fetching information from a remote http: address and want something to print if the fetch fails. Could someone verify if this basic code is correct - (the bit after the IF function)

    $file = fopen("$GrabURL", "r");
    $rf = fread($file, 20000);
    $grab = eregi("$GrabStart(.*)$GrabEnd", $rf, $printing);

    IF ($printing == "")
    {
    echo Sorry no results found;
    }
    ELSE
    {
    echo $printing[1];
    }


    Does this look correct? I'm unsure whether the IF line should relate to $printing, $printing[1] or $file.

    Could someone clarify

    Cheers

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Freddy - noticed you on the board - can you help!!!

    Cheers
    <Edited by ChilliBoy on 12-04-2000 at 11:43 AM>

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that poses an interesting problem, I was messing around with your script a bit, and I found that if there is nothing between the $startGrab and $endGrab the ereg doesn't fail but it doesn't return anything. Do you think you will always know whether or not there is data between the start and end grab marks if so then you can just use

    <?

    ########################
    ## Mandatory Setting ##
    ########################

    $GrabURL = "http://localhost/louie/sample.htm"; //- Complete URL of the page your grabbing from!
    $GrabStart = "<!--This is where I want the grab to start -->";
    $GrabEnd = "<!--This could be used as grab end -->";
    $Linkstart = "<p>";
    $Linkend = "<br>";
    $Textstart = "<font color=\"";
    $Textend = "</p>";
    $Numberoflinkstoprint = "3";

    ########################################
    ## Edit str_replace if required below ##
    ########################################
    //$file = file($GrabURL);
    $file = fopen($GrabURL, "r");
    $rf = fread($file, 20000);
    //$lines = implode("", $file);
    $grab = eregi("$GrabStart(.*)$GrabEnd", $rf, $printing);
    if ($grab) {
    // $printing[1] = str_replace("", "", $printing[1]);
    $pieces = explode($Linkstart, $printing[1]);
    //$pieces = explode($Linkstart, $printing[0]);
    $count=count($pieces);
    for($x=1;$x<=$Numberoflinkstoprint;$x++){
    eregi("(.*)$Linkend",$pieces[$x],$Links );
    eregi("$Linkend(.*)$Textend",$pieces[$x],$Text );
    echo $Links[1]."<br>";
    echo $Text[1]."<br>";
    }
    }
    else {
    print "Failure getting data";
    }
    fclose($file);
    ####################
    ## End of Script ##
    ####################
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well my main concern is this:

    If I have several similar scripts on one page - all fetching from different URL's. Say one of these URL's is having server problems - so the script would obviously fail in the fetch. What I'm most concerned about is that if just one of the URL fetches fail - the whole page will fail and print up an error page. I'm not particularly sure in how this would actually work, but I want to aviod the above at all cost.

    It would be great if I could get something to print if either:

    The Fetch Fails because the server I'm fetching from is having problems.

    Or

    The fetch fails because the fetched section has changed so that the grab cannot work.

    Ummmmmm????

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could just suppress error reporting and do it this way
    It would keep the error from showing up on the screen if the if the fetch fails. But it would let the rest of the page run fine.

    ########################
    ## Mandatory Setting ##
    ########################

    $GrabURL = "http://localhost/loie/sample.htm"; //- Complete URL of the page your grabbing from!
    $GrabStart = "<!--This is where I want the grab to start -->";
    $GrabEnd = "<!--This could be used as grab end -->";
    $Linkstart = "<p>";
    $Linkend = "<br>";
    $Textstart = "<font color=\"";
    $Textend = "</p>";
    $Numberoflinkstoprint = "3";

    ########################################
    ## Edit str_replace if required below ##
    ########################################
    //$file = file($GrabURL);
    $file = @fopen($GrabURL, "r");
    $rf = @fread($file, 20000);
    //$lines = implode("", $file);
    $grab = eregi("$GrabStart(.*)$GrabEnd", $rf, $printing);
    if ($grab) {
    // $printing[1] = str_replace("", "", $printing[1]);
    $pieces = explode($Linkstart, $printing[1]);
    //$pieces = explode($Linkstart, $printing[0]);
    $count=count($pieces);
    for($x=1;$x<=$Numberoflinkstoprint;$x++){
    eregi("(.*)$Linkend",$pieces[$x],$Links );
    eregi("$Linkend(.*)$Textend",$pieces[$x],$Text );
    echo $Links[1]."<br>";
    echo $Text[1]."<br>";
    }
    }
    else {
    print "Failure getting data";
    }
    @fclose($file);
    ####################
    ## End of Script ##
    ####################
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers

    Your Cat does it again!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •