SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast MadDog31's Avatar
    Join Date
    Nov 2003
    Location
    Wilmington, NC
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question get_file_contents URL issue...

    Hey all,

    Going off of what I did last week on the get_file_contents of a stock quote, a buddy of mine wondered if I could use PHP to pull a song name from his Shoutcast server site to display directly on his homepage.

    Here's the skinny:

    PHP Code:
    <html> 
    <head> 
      <title>Current Song Playing</title> 
    </head> 
    <body> 
    <?php 

    // Set variables for the station URL and name
        
    $stationurl 'http://peatboxradio.darktech.org:8000/listen.pls';
        
    $stationname 'Peatbox Radio';

        echo 
    '<h1>Current Song Playing on '$stationname '</h1>'

    // Get information
        
    $theurl   'http://peatboxradio.darktech.org:8000'
        
    $contents function_exists('file_get_contents') ? file_get_contents($theurl) : implode(''file($theurl)); 
        if (!
    $contents) { 
            die(
    'Unknown information'); 
        } 

        
    $pattern '~Current Song: </font></td><td><font class=default><b>(\$[a-z]+)</b> ~iUs';

        if (
    preg_match($pattern$contents$match)) { 
            
    $currentsong $match[1]; 
            echo 
    '<p> Current song playing is: <strong>' $currentsong '</strong></p>'
        } else {
            echo 
    'Could not retrieve value <br />';
        }

    // Acknowledge source 
        
    echo 'This information retrieved from <br /> 
               <a href="'
    .$theurl.'">'.$theurl.'</a><br /> 
               on ' 
    date('l jS F Y g:i a T'); 
    ?> 
    </body> 
    </html>
    I'm trying to pull the 'Current Song' playing that's listed in this URL:

    http://peatboxradio.darktech.org:8000/

    I'm using file_get_contents to read in that page, however, it only works when I ditch the ':8000' from the URL, so I'm assuming there's issues w/ utilizing pages that are via certain ports. I know my code my be sketchy elsewhere (such as $pattern) but I just want to get past this port issue first. The above URL is the only way to get to the current song also.

    I'm gonna head over to the Manual to see if I can find anything there, but wanted to post here to see if anyone has had issues w/ this before.

    Ian
    "It's way better to have 100 idiot clients than to have one idiot boss."

  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use file_get_contents() on a url, it goes to port 80. You will have better luck with fsockopen. That way you can open port 8000.

    Aaron
    Aaron Brazell
    Technosailor



  3. #3
    SitePoint Enthusiast MadDog31's Avatar
    Join Date
    Nov 2003
    Location
    Wilmington, NC
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll have to do some reading up on fsockopen...loops pretty in-depth.

    Ian
    "It's way better to have 100 idiot clients than to have one idiot boss."

  4. #4
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not hard.

    Pseudocode:

    fsockopen(location,port);
    read contents into string;
    close fsockopen;

    use string.
    Aaron Brazell
    Technosailor



  5. #5
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $fp fsockopen('peatboxradio.darktech.org'8000) or die('Connection failed');
    $request "GET /listen.pls HTTP/1.0\r\nHost: peatboxradio.darktech.org\r\nConnection: close\r\n\r\n";
    fputs($fp$request);    
    $found   false;
    $header  '';
    $content '';
    while (!
    feof($fp)) {
        
    $data fgets($fp4096);
        if (!
    $found) {
            
    $header .= $data;
            if (
    false !== ($data strstr($header"\r\n\r\n"))) {
                
    $data substr($data4);
                
    $found true;
            }
        }
        if (
    $found)
            
    $content .= $data;
    }
    fclose($fp);

    // $content should now contain the file 

  6. #6
    SitePoint Enthusiast MadDog31's Avatar
    Join Date
    Nov 2003
    Location
    Wilmington, NC
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I'm getting closer after that code however I'll have to play with it some more to get it to work properly. Thanks to both of you for assistance as always. I'll have to play w/ this some more when I have the opportunity.

    Meanwhile, I need to learn about sessions. On Chapter 20 in the PHP/MySQL Web Development book.

    Ian
    "It's way better to have 100 idiot clients than to have one idiot boss."


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
  •