1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php script problem

Discussion in 'PHP' started by wikech, Jun 21, 2009.

  1. #1
    I copy the php script in the web. I found that it has the following error massage “Warning: file(http://www.hko.gov.hk/textonly/forecast/chinesewx.htm) [function.file]: failed to open stream: HTTP request failed! in /var/www/html/weather1.php on line 18” when I use in my web server.

    However, it works if I change the code from “$weatherURL=' http://www.hko.gov.hk/textonly/forecast/chinesewx.htm';” to “$weatherURL=' text.html';” (text.html stored the text in the http://www.hko.gov.hk/textonly/forecast/chinesewx.htm).

    Can anyone help me.

    <?php

    $weatherURL=' http://www.hko.gov.hk/textonly/forecast/chinesewx.htm';

    $file=file($weatherURL);
    $temperature = $file[15]; //line 15
    $temperature = ereg_replace("氣 溫 : ","","$temperature");
    $temperature = ereg_replace(" 度","","$temperature");
    $temperature = ereg_replace("\n","","$temperature");

    echo $temperature;

    ?>
     
    wikech, Jun 21, 2009 IP
  2. TeamRadiance

    TeamRadiance Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The problem is that the file() function requires a path within the server.

    All you need to do is replace $file=file($weatherURL); with
    $file=file_get_contents($weatherURL);
     
    TeamRadiance, Jun 21, 2009 IP
  3. wikech

    wikech Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The error is still happened.

    The error massage is
    "Warning: file_get_contents(http://www.hko.gov.hk/textonly/forecast/chinesewx.htm) [function.file-get-contents]: failed to open stream: HTTP request failed! in /var/www/html/weather1.php on line 18"
     
    wikech, Jun 21, 2009 IP
  4. Sudoku-Master

    Sudoku-Master Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I use allways the php webclient class Snoopy to grap websites. But use the Snoopy class from wordpress, that's better maintenced than the original Snoopy class...
     
    Sudoku-Master, Jun 22, 2009 IP
  5. TeamRadiance

    TeamRadiance Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think I see the problem, you need to take out that extra space character at the beginning of the URL. Like so:
    $weatherURL = '"http://www.hko.gov.hk/textonly/forecast/chinesewx.htm";
     
    TeamRadiance, Jun 22, 2009 IP
  6. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Are you sure you have outgoing connections enabled ?
     
    stOK, Jun 22, 2009 IP
  7. wikech

    wikech Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I check that the outgoing connections is enabled and I use the code like "$weatherURL = '"http://www.hko.gov.hk/textonly/forecast/chinesewx.htm";". The problem is still happened.
     
    wikech, Jun 24, 2009 IP
  8. dweebsonduty

    dweebsonduty Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    Digital Goods:
    1
    #8
    If this happens to me I just use curl.

    
    $url="weathersite"
    $ch = curl_init();
                $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ".
                              "Windows NT 5.0)";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
                curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
                curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
                curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
                curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
                curl_setopt( $ch, CURLOPT_URL, $url );
                curl_setopt( $ch, CURLOPT_REFERER, $ref );
                curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
                $html = curl_exec($ch);
                curl_close($ch);
    PHP:
    Then parse $html
     
    dweebsonduty, Jun 27, 2009 IP