Meet Singles on True.com
view all · advertise on glowfoto
Home | My Glowfoto | Top20 | Random Photo | Map | Chat | Slideshows | Groups | Floaters | FAQ | Contact | Browse | Search | More
username: password: [ sign up! ]
Glowfoto Upload API
If you wish to create your own uploader or integrate Glowfoto image hosting into your own site, you can use the Glowfoto Upload XML API.

To use the API, you will make a request to two forms -- the first to get the address of a valid form to post your data to, and the second to actually upload the image. Both forms return their data as simple XML.

NOTE: if you just want something you can copy and paste to get going right away, skip down to the PHP example at the bottom of this page.

STEP ONE: Request a valid upload server

To get the URL of the upload server you should use, simply request the following URL:

http://www.glowfoto.com/getserverxml.php

You will receive an XML document that looks something like the following:

<getserver>
  <uploadform>http://img5.glowfoto.com/uploadxml.php</uploadform>
</getserver>
The data contained within the <upload> tag is the full URL of the form to which you should post your upload. When building the HTTP request that contains the image, you should POST the data to the form with enctype="multipart/form-data" (and of course, the action should be the URL you received above).

Note that getserverxml.php MUST be called before EACH post! You should not assume that the upload URL will be the same EVER. These URLs always change, and sometimes retire. Again, ALWAYS request a valid upload URL before posting, every single time!

STEP TWO: Post the data

Now that you have a valid upload URL and have begun to build your request, you need to fill in the POST data. Following is a list of POST variables and the data they should contain:

  • image - the POST variable containing your image data.
  • type - reserved. always set this to file
  • thumbsize - the width of the bounding square defining the maximum size of the thumbnail. This should be set to either 100, 200, 300, or 400. If you don't use one of these values, it will default to 200.
When your request is complete, POST it to the URL you received in step one. You will receive back another XML document that looks like the following:

<upload>
  <thumburl>http://img5.glowfoto.com/images/2007/04/03-0024176846T.jpg</thumburl>
  <imageurl>http://www.glowfoto.com/static_image/03-002417L/6846/jpg/04/2007/img5/glowfoto</imageurl>
  <codes>http://www.glowfoto.com/getcode.php?srv=img5&img=03-002417L&t=jpg&rand=6846&m=04&y=2007</codes>
</upload>
The URL of the thumbnail image is stored in the <thumburl> tag, and the URL of the full size image is stored in the <imageurl> tag. The <codes> tag contains a link to a page on which several types of code for the uploaded file are preformatted. If you do not wish to generate link codes in your application, you can redirect your users to this page to retrieve their link codes.

Note that <thumburl> points to an image, while <imageurl> points to a page. It is your responsibility to construct the BBCODE, wiki markup, HTML or direct link that your application requires. Glowfoto is used to build and host clickable thumbnails, so the code you construct should be an <img> tag wrapped in an <a> tag (or, in the case of BBCODE, an [IMG] tag wrapped in a [URL] tag).

WOOPS! Errors!

If something goes wrong, the XML file you receive after posting will contain an <error> tag, and an error message will be contained within that tag. There are two possible error messages:

  • No File Uploaded
  • Invalid File Uploaded
Both error messages indicate complete failure, and the <thumburl>, <imageurl> and <codes> tags should be disregarded.

Sample: Using the API with PHP

Here is a simple example of how to post a file on the local server to Glowfoto using the API, parse the returned XML, and construct the HTML and BBCODE from the XML.

This should work right out of the box. All you need to do is set $img to point to the file you want to post to glowfoto.
<?php

// A couple globals to hold the thumbnail and static URLs of the image
$thumburl  = "";
$staticurl = "";

// a global representing the tag currently open
$tag = "";

function uploadToglowfoto($filename) {
       //---------------------------------------------
       // This function posts the image $filename
       // to a glowfoto image server
       //---------------------------------------------

       // get the form URL to post to
       $xmlString = implode("",file('http://www.glowfoto.com/getserverxml.php'));
       $xmlData = explode("\n", $xmlString);
       $postURL = $xmlData[2];
       $postURL = str_replace("<uploadform>","",$postURL);
       $postURL = str_replace("</uploadform>","",$postURL);
       $postURL = rtrim(ltrim($postURL));

       // post with curl

       $ch = curl_init($postURL);

       $post['type']='file';
       $post['image']='@'.$filename;

       // You can set this to either 100,200,300, or 400 to specify the size of the thumbnail
       $post['thumbsize']="400";   

       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HEADER, false);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_TIMEOUT, 240);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
       curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));

       $result = curl_exec($ch);
       curl_close($ch);

       return $result;
}

function startElement($parser, $tagName, $attrs) {
        //---------------------------------------------
        // Make a note of which tag just opened
        //---------------------------------------------

        global $tag;

        $tag = $tagName;
}

function endElement($parser, $tagName) {
        //---------------------------------------------
        // We don't need to do anything special
        // when a tag closes
        //---------------------------------------------
}

function characterData($parser, $data) {
        //---------------------------------------------
        // Store the data in the appropriate tag
        //---------------------------------------------

        global $tag, $thumburl, $imageurl;

        if ($tag == "THUMBURL")         $thumburl .= $data;
        else if ($tag == "IMAGEURL")    $imageurl .= $data;

}

// Set a file to upload
// THIS IS THE KEY PART TO MODIFY FOR YOUR OWN USE
$img = "/home/me/image.gif";

// $xmlreturn contains the returned XML
$xmlreturn = uploadToglowfoto($img);

// Now we need to parse the URL of the thumbnail
// and the URL of the static page

// Create an XML parser
$xml_parser = xml_parser_create();

// Set the functions to handle opening and closing tags
xml_set_element_handler($xml_parser, "startElement", "endElement");

// Set the function to handle blocks of character data
xml_set_character_data_handler($xml_parser, "characterData");

// Parse the data
xml_parse($xml_parser, $xmlreturn, true);

// Free up memory used by the XML parser
xml_parser_free($xml_parser);

// Now our URLs are stored in $thumburl and $imageurl
// Strip out any trailing whitespace
$thumburl = rtrim($thumburl);
$imageurl = rtrim($imageurl);

// Build the BBCODE and HTML code
$BBCODE = "[URL=".$imageurl."][IMG]".$thumburl."[/IMG][URL]";
$HTML = "<a href=$imageurl<>img src=$thumburl></a>";

// Note that you might need to pass $thumburl into htmlspecialchars so
// it will not be interpreted as HTML when you present the code to
// your users.  If so, give them $presentableHTML
$presentableHTML = htmlspecialchars($HTML);

?>

You might want to paste this code into its own file (minus the line that sets $img), and then include it after your own script wants to post an image to glowfoto. Just set $img to point to the file you want to upload, and then include this script immediately. You can then use the BBCODE and HTML as desired.
© 2004-2007 Glowdot Productions, Inc. All Rights Reserved
Terms of Service · Advertise · About · Company · Blog · Report Abuse