SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get_contents failing

    Hey all. I have a section of code to grab the contents of a file. This works about 70% of the time, the other 30%, i get the error
    Warning: file_get_contents(): HTTP request failed! HTTP/1.1 404 /en/pplayer.vm in /home/godsfshr/public_html/sig/parse.php on line 3

    Warning: file_get_contents(http://eq2players.station.sony.com/e...erId=105369104): failed to open stream: Permission denied in /home/godsfshr/public_html/sig/parse.php on line 3
    Is there a different way to do this without having to worry about the error coming up randomly?

    Here is the section of code I use:
    PHP Code:
        if($raw_data file_get_contents("http://eq2players.station.sony.com/en/player.vm?characterId=$charid"))  {
            
    // Verify that the guild name is found on the webpage
            
    if(!($guild_name_loc strpos($raw_data,"$char</span>")))  {
                echo 
    "Unable to find $char on the website";
                             exit;
            } 
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  2. #2
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone know a solution for this?
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  3. #3
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try the famous fopen, fread and fclose way, but file_get_contents does exactly that. Not sure why the error occurs, it appears as 404, which is "not found". Does it work 100% ff you try to access that file with your browser?

  4. #4
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *
    Last edited by godsfshrmn; May 6, 2005 at 08:03.
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  5. #5
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems like the same error occurs in your php. Is it possible that your connection to that server is somewhat 'unreliable'?

    I am only guessing here, but there may be something related to a time-out when opening a remote file in php. Never heard of such a thing, but then again, I haven't used fopen() and family on remote files much ...

    Can't help you much more on this, sorry

  6. #6
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, is there a way I could get the code to execute until the data is successfully grabbed? I KNOW the page exists. The page is checked for validity when a user registers on the website.

    When file_get_contents does not work, i noticed it is retured false. I can't figure out a loop to fix this.
    Last edited by godsfshrmn; Apr 26, 2005 at 16:14.
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  7. #7
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess you could do something like this:
    PHP Code:
    $contents '';
    $max_loops 5;
    for (
    $i 0$i $max_loops$i++)
    {
      
    $contents file_get_contents(...);
      if (
    $contents)
        break;
      else
        
    sleep(1); <- 1 second delayto not hit on the server too much
    }

    if (
    $contents)
      echo 
    $contents;
    else
      echo 
    "no contents"
    after the loop you'll know if connection is ok or not. You can loop more than 5 times, but this will create large delays when your script doesn't get the contents and retries. You could remove the sleep delay, but i guess you'd be hitting the server too fast then.

    Actually I think there must be a better solution than a loop for this. If a link is there, it should open. Are you sure it's not your connection's or provider's problem?

    Regards

  8. #8
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it might be the server I am trying to grab the info from. I know of a very similar script that does almost the same thing, and occasionally I get the same error with it also. I just wonder if trying to connect until it actually works would solve the problem?
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  9. #9
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe it would, but you must not do such a loop. If the server is actually down, your script would loop endlessly. You need to have a max loop limit and it must be as low as possible.

  10. #10
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When using that above, is the file's contents retrieved every time $contents is called later in the file, or is it retreived that one time in the for statement?
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, the filename in the warning is different from the filename you're requesting.
    - noobus in perpetuum -

  12. #12
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eerok
    Hmm, the filename in the warning is different from the filename you're requesting.
    The file being requested had a varable at then end of it. I assume the variable is the long string of numbers at the end of the error message file.

  13. #13
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by godsfshrmn
    When using that above, is the file's contents retrieved every time $contents is called later in the file, or is it retreived that one time in the for statement?
    Get the file only once per request, and only if it's needed. You can use the same data in the same request several times. Maybe you could also cache the content and not even request the file for each request.


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
  •