0

I'm not a programmer, but I did study C for a while. I can figure out how to find functions, look at their syntax, see examples and then try them.

So here's my siutation. I want the url of the output of a web site into a php variable. Here's the setup:

The following url returns an image. I want that image's url in a php variable.
http://newpics.huntsvillecarscene.com/photos/random.mg?AlbumID=1626430&Size=Th&AlbumKey=eBBAU&rand=8075

I've tried http_get, but apparently it's an undefined function on my server. I'm not sure if that's because my host doesn't have it enabled or because something isn't right in my syntax. Here is the line of code:

$randomImageURL = http_get("http://$photographer.huntsvillecarscene.com/photos/random.mg?AlbumID=$galleryNumber&Size=Th&AlbumKey=$galleryKey&rand=8075", array("timeout"=>1), $randomImageGetInfo);

I've read about file_get_contents, but that looks like it will actually get the file contents. I've looked into 'system' and executing wget, but that also attempts to retrieve the actual file.

Any assistance appreciated.

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Samir
0

It didn't work. :(

Looks like it returned the image file because there was a lot of garbage on my putty session and the line where I print the variable doesn't show anything.

I found this in a comment here: http://us.php.net/function.fopen
This seems like it might work, but I can't really figure out if the code will read in just the url, or a whole file:

<?php
#usage:
$r = new HTTPRequest('http://www.example.com');
echo $r->DownloadToString();

class HTTPRequest
{
    var $_fp;        // HTTP socket
    var $_url;        // full URL
    var $_host;        // HTTP host
    var $_protocol;    // protocol (HTTP/HTTPS)
    var $_uri;        // request URI
    var $_port;        // port
   
    // scan url
    function _scan_url()
    {
        $req = $this->_url;
       
        $pos = strpos($req, '://');
        $this->_protocol = strtolower(substr($req, 0, $pos));
       
        $req = substr($req, $pos+3);
        $pos = strpos($req, '/');
        if($pos === false)
            $pos = strlen($req);
        $host = substr($req, 0, $pos);
       
        if(strpos($host, ':') !== false)
        {
            list($this->_host, $this->_port) = explode(':', $host);
        }
        else
        {
            $this->_host = $host;
            $this->_port = ($this->_protocol == 'https') ? 443 : 80;
        }
       
        $this->_uri = substr($req, $pos);
        if($this->_uri == '')
            $this->_uri = '/';
    }
   
    // constructor
    function HTTPRequest($url)
    {
        $this->_url = $url;
        $this->_scan_url();
    }
   
    // download URL to string
    function DownloadToString()
    {
        $crlf = "\r\n";
       
        // generate request
        $req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf
            .    'Host: ' . $this->_host . $crlf
            .    $crlf;
       
        // fetch
        $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
        fwrite($this->_fp, $req);
        while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
            $response .= fread($this->_fp, 1024);
        fclose($this->_fp);
       
        // split header and body
        $pos = strpos($response, $crlf . $crlf);
        if($pos === false)
            return($response);
        $header = substr($response, 0, $pos);
        $body = substr($response, $pos + 2 * strlen($crlf));
       
        // parse headers
        $headers = array();
        $lines = explode($crlf, $header);
        foreach($lines as $line)
            if(($pos = strpos($line, ':')) !== false)
                $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));
       
        // redirection?
        if(isset($headers['location']))
        {
            $http = new HTTPRequest($headers['location']);
            return($http->DownloadToString($http));
        }
        else
        {
            return($body);
        }
    }
}
?>
0

That would only help me get the file, which is not what I'm interested in.

I was finally able to find a solution, which was via the snippet of code found here:
http://snipplr.com/view.php?codeview&id=1180

What I needed was the base URI, and this could be found in the header before redirection.

I was able to take this function and get what I wanted. Thank you for the replies.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.