After a few days of messing around with the Cafe Press api I finally got a working script to upload images, apply tags to the images and create products from the images. I know that the cafe press people are working to up date the apis and I appreciate that but it does make for some confusion when using them. So I decided to make my efforts known to those that may need them one day....
To start, get an api key. Here is the page http://cafepressdn.com/api/content/index.aspx . Once you get that key save it you will need it for everything, well not everything but most of the calls need this key like getting a user token. This example assumes that you have an account with cafe press and that you will be uploading your data to the default file. The code is written in PHP and I was just running the script from the command line so any enhancements are yours....
I used the cUrl library to communicate with the CP servers. I just wrote a function called send_data that does all the cUrl setup and sending/receiving of the data. Here is what I did:
And another great function for PHP is xml2array to clean up the XML response values and get to data easier (well for me it was easier). Check out the code at http://www.bin-co.com/php/scripts/xml2array/index.php.
First off get the user token:
function get_userToken(){
$AuthUrl = "http://api.cafepress.com/authentication.getUserToken.cp?v=3&appKey=".<ADD YOUR API KEY HERE>
$AuthUrl .= "&email=".<ADD THE EMAIL ADDRESS YOU SIGNED UP WITH HERE>."&password=".<YOUR CAFE PRESS PASSWORD HERE>;
$userToken = $this->send_data($AuthUrl, FALSE);
return $userToken;
}
One note to check is the appkey variable is case sensitive so make sure you make it appKey. And though the singature here: http://cafepressdn.com/api/content/Authentication.aspx shows username between appKey and password the real parameter for this is email as shown in the Parmameters box....
Next I take the user token and the application key to upload the images to cafe press
function upload_image($ut,$path){
$postData = array();
$postData['userToken'] = <ADD YOUR USER TOKEN HERE>
$postData['appKey'] = <ADD YOUR APPLICATION KEY HERE>
$postData['folder'] = "Images";
$x = $this->xml2array($response);
return $x['values']['value'];//this is the image id
}
I pass into this function the user token and the path to the image I want to upload. Beacuse this is a POST operation I set up cURL to handle that...
I take the response from the upload and pull out the image id value to use for setting the tags and creating a product. NOTE that the folder option is 'folder' and NOT 'folderName' as it shows in the api documents....
Now to uplaod tags to associate with the image.
function set_tags($ut, $did, $tags){
$postData = array();
$postData['appKey'] = <ADD YOUR USER TOKEN HERE>
$postData['userToken'] = <ADD YOUR APPLICATION KEY HERE>
$postData['designIds'] = <ADD THE IMAGE ID YOU GOT FROM THE RESPONSE FROM THE IMAGE UPLOAD>
$postData['tags'] = $tags;
I decided that in some cases I had a large number of tags that I would pass the data for this call as a POST instead of a GET, so the cURL set up is the same as with the image upload. Not much to this one...
Now creating a product.... This one got me, after looking through the fourms I found that I did not need all that extra crap in the XML just the <PRODUCT> tags filled out... much cleaner .
function create_product($ut,$id){
$prodXML = "<?xml version=\"1.0\"?>";
$prodXML .= "<product merchandiseId="."\""<ADD THE ID OF THE MERCHANDISE YOU WANT TO USE>"\"";
$prodXML .= " sellPrice=\"21.99\"";
$prodXML .= " description=\"test\"";
$prodXML .= " storeId=\"<THE STORE ID IS THE NAME OF THE STORE NOT JUST ITS NUMBER>\"";
$prodXML .= " sectionId=\"0\">";
$prodXML .= "<mediaConfiguration dpi=\"100\"";
$prodXML .= " height=\"10\"";
$prodXML .= " width=\"10\"";
$prodXML .= " name=\"FrontCenter\"";
$prodXML .= " designId="."\"".<THIS IS THE IMAGE ID THAT YOU GOT IN THE UPLOAD>."\"";
$prodXML .= " />";
$prodXML .= "</product>";
$prodXMLurlEncode = urlencode($prodXML);
$url ="http://api.CafePress.com/product.save.cp?v=3&appKey=".<ADD YOUR APPLICAION KEY HERE>."&userToken=".<ADD USER TOKEN HERE>."&value=".$prodXMLurlEncode;
After a few days of messing around with the Cafe Press api I finally got a working script to upload images, apply tags to the images and create products from the images. I know that the cafe press people are working to up date the apis and I appreciate that but it does make for some confusion when using them. So I decided to make my efforts known to those that may need them one day....
To start, get an api key. Here is the page http://cafepressdn.com/api/content/index.aspx . Once you get that key save it you will need it for everything, well not everything but most of the calls need this key like getting a user token. This example assumes that you have an account with cafe press and that you will be uploading your data to the default file. The code is written in PHP and I was just running the script from the command line so any enhancements are yours....
I used the cUrl library to communicate with the CP servers. I just wrote a function called send_data that does all the cUrl setup and sending/receiving of the data. Here is what I did:
function send_data ($url, $verbose){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($verbose == TRUE)
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$result= curl_exec($ch);
curl_close($ch);
return $result;
}
And another great function for PHP is xml2array to clean up the XML response values and get to data easier (well for me it was easier). Check out the code at http://www.bin-co.com/php/scripts/xml2array/index.php.
First off get the user token:
function get_userToken(){
$AuthUrl = "http://api.cafepress.com/authentication.getUserToken.cp?v=3&appKey=".<ADD YOUR API KEY HERE>
$AuthUrl .= "&email=".<ADD THE EMAIL ADDRESS YOU SIGNED UP WITH HERE>."&password=".<YOUR CAFE PRESS PASSWORD HERE>;
$userToken = $this->send_data($AuthUrl, FALSE);
return $userToken;
}
One note to check is the appkey variable is case sensitive so make sure you make it appKey. And though the singature here: http://cafepressdn.com/api/content/Authentication.aspx shows username between appKey and password the real parameter for this is email as shown in the Parmameters box....
Next I take the user token and the application key to upload the images to cafe press
function upload_image($ut,$path){
$postData = array();
$postData['userToken'] = <ADD YOUR USER TOKEN HERE>
$postData['appKey'] = <ADD YOUR APPLICATION KEY HERE>
$postData['folder'] = "Images";
$uploadUrl = "http://upload.cafepress.com/image.upload.cp";
$postData['cpFile1'] = "@".realpath($Path);
//DEBUG print_r($postData);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uploadUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// curl_setopt($ch, CURLOPT_VERBOSE,1);
$response = curl_exec($ch);
curl_close($ch);
$x = $this->xml2array($response);
return $x['values']['value'];//this is the image id
}
I pass into this function the user token and the path to the image I want to upload. Beacuse this is a POST operation I set up cURL to handle that...
I take the response from the upload and pull out the image id value to use for setting the tags and creating a product. NOTE that the folder option is 'folder' and NOT 'folderName' as it shows in the api documents....
Now to uplaod tags to associate with the image.
function set_tags($ut, $did, $tags){
$tagUrl = "http://api.cafepress.com/design.tagDesigns.cp";
$postData = array();
$postData['appKey'] = <ADD YOUR USER TOKEN HERE>
$postData['userToken'] = <ADD YOUR APPLICATION KEY HERE>
$postData['designIds'] = <ADD THE IMAGE ID YOU GOT FROM THE RESPONSE FROM THE IMAGE UPLOAD>
$postData['tags'] = $tags;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tagUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_VERBOSE,1);
$respone = curl_exec($ch);
curl_close($ch);
//DEBUG echo $result;
}
I decided that in some cases I had a large number of tags that I would pass the data for this call as a POST instead of a GET, so the cURL set up is the same as with the image upload. Not much to this one...
Now creating a product.... This one got me, after looking through the fourms I found that I did not need all that extra crap in the XML just the <PRODUCT> tags filled out... much cleaner .
function create_product($ut,$id){
$prodXML = "<?xml version=\"1.0\"?>";
$prodXML .= "<product merchandiseId="."\""<ADD THE ID OF THE MERCHANDISE YOU WANT TO USE>"\"";
$prodXML .= " sellPrice=\"21.99\"";
$prodXML .= " description=\"test\"";
$prodXML .= " storeId=\"<THE STORE ID IS THE NAME OF THE STORE NOT JUST ITS NUMBER>\"";
$prodXML .= " sectionId=\"0\">";
$prodXML .= "<mediaConfiguration dpi=\"100\"";
$prodXML .= " height=\"10\"";
$prodXML .= " width=\"10\"";
$prodXML .= " name=\"FrontCenter\"";
$prodXML .= " designId="."\"".<THIS IS THE IMAGE ID THAT YOU GOT IN THE UPLOAD>."\"";
$prodXML .= " />";
$prodXML .= "</product>";
$prodXMLurlEncode = urlencode($prodXML);
$url ="http://api.CafePress.com/product.save.cp?v=3&appKey=".<ADD YOUR APPLICAION KEY HERE>."&userToken=".<ADD USER TOKEN HERE>."&value=".$prodXMLurlEncode;
$response = $this->send_data($url, TRUE);
//DEBUG echo "SAVED: " .$response."\n";
}
This one I ran as a GET.. I just had to remember to encode the XML string to be URL friendly...
Thats really it. Let me know if I left something out...
calocan – 5 months ago
Thanks for posting that. I'll put a link to it in the docs.