The Printfection API lends itself really well to creating absolutely unique shirt designs from user created content. You might have a website where people can upload something and then manipulate it, or you might be creating your own images. Either way, you need to get the images uploaded to Printfection as part of the product creation process.
The Printfection API has a built in method for uploading images to your account, "Printfection.Images.upload", which is a little bit different from the other methods. Firstly, it must be sent as a POST request, and unlike the other methods, it has it's own URL for the request, http://upload.printfection.com/api_upload.php.
The upload method has four specific parameters that are sent with the request, the data of the file, the name for the image, the image set that you want to put it in and optionally, a comma separated list of keywords to associate with the image. As usual, you also need to send the global, api parameters and the api signature with the request.
Because we are sending file data with our parameters, we need a method for attaching the file to the request. You could use a form, however in PHP, the most flexible method is to use cURL.
Like all of our other uses of methods, we need to set up our parameters. First, the global parameters
$api_key = your_api_key;
$secret = your_secret_key;
$sessionID = your_sessionID_recevied_from_authentication;
$method = "Printfection.Images.upload";
Then the parameters for our upload:
$uploadfile = "/home/public_html/images/HelloWorld.jpg";
$isetid = "50000";
$iname = "Hello World Image";
Then we can calculate the api_sig using our alphabetized list of parameters. Note that the actual image file is not part of the key:
$api_sig_tmp = "api_key=".$api_key."imagesetid=".$isetid."method=".$method."name=".$iname."session_key=".$sessionID."version=1.0".$secret;
$api_sig = md5($api_sig_tmp);
Sending the cURL request
Now that we have all our parameters and our file, we can send the request.
$post_data = array();
$post_data['api_key'] = $api_key;
$post_data['api_sig'] = $api_sig;
$post_data['session_key'] = $sessionID;
$post_data['method'] = $method;
$post_data['name'] = $iname;
$post_data['imagesetid'] = $isetid;
$post_data['version'] = "1.0";
$post_data['file'] = "@$uploadfile";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://upload.printfection.com/api_upload.php" );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
In this example, $postResult is the returned XML from your request, which includes the imageid for your newly uploaded image:
<?xml version="1.0" encoding="UTF-8"?>
You will use the imageid for the next step of actually creating the product.