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.

Well file_get_contents() is the simplest solution and another solution is curl. However file_get_contents() only works if you place the http:// at the beginning of the address and the complete url. So for example try the following:

file_get_contents('http://newpics.huntsvillecarscene.co...BBAU&rand=8075');

Hope that helps.

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);
        }
    }
}
?>

Well looks like you will need to have a separate file to refer to. To do this first create a new file with the following code:
image.php

<? //this is the entire page
echo file_get_contents('http://newpics.huntsvillecarscene.com/photos/random.mg?AlbumID=1626430&Size=Th&AlbumKey=eBBAU&rand=8075');
?>

Then make a html image take to that file like the following:
index.php

<?
echo '<img src="image.php">';
?>

But make sure those 2 files are in the same directory.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.