How to Use PHP Curl to Post to classic ASP on IIS

I recently had a project at my 9 to 5 that involved the need to combine several product XML data exports from an existing shopping cart and migrate them to a different shopping cart. The “different shopping cart” was written in classic ASP.

The Project Task List:

Combine several XML files to dump data into an adhoc created SQLite Database.

Post product data to existing classic ASP scripts that would handle putting the products into the new shopping cart automatically.

Post Data will include uploaded Image Data for an MSSQL database.

Is this how I would have liked to do this? No. But circumstances as they were it is what had to be done.

Everything was trucking along just fine until it came time to post the data to the classic ASP script running on some ancient version of IIS.

Using plain ole curl request parameters without a lot of extra header data was getting me NO WHERE. I was getting a whole range of errors. (I put this post off for far too long so I don’t remember what errors I was getting or what sources I used to piece together my working functions.) I had to post uploaded file data (for product images) and also just plain posted text data fields (title,descriptions,weight, price). Here are the functions I used for both.

I created a file called, “curlfunctions.php” that include these three functions. I included it when I was looping through the product data queries from the SQLite database.

Function 1: CURLOPT_POSTFIELDS prep function

This function takes any array ($vars) and puts it into the linear separated (with “&”) string that CURL likes. In this case I just dumped in the whole $_POST array. Note: I did NOT use this function for the image posting. Just the text.

function postVars($vars,$sep='&amp;') { $str = ''; foreach( $vars as $k => $v) { if(is_array($v)) { foreach($v as $vk=>$vi) { $str .= $k.'[]'.'='.urlencode($vi).$sep; } } else { $str .= $k.'='.urlencode($v).$sep; } } return substr($str, 0, -1); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 function postVars ( $vars , $sep = '&amp;' ) { $str = '' ; foreach ( $vars as $k =& gt ; $v ) { if ( is_array ( $v ) ) { foreach ( $v as $vk =& gt ; $vi ) { $str . = $k . '[]' . '=' . urlencode ( $vi ) . $sep ; } } else { $str . = $k . '=' . urlencode ( $v ) . $sep ; } } return substr ( $str , 0 , - 1 ) ; }

Function 2: addproduct() regular text fields

The trick with Classic ASP/IIS is all this extra header data. This is what worked for me to make the data accessible by the Classic ASP script through the Request.Form(“FormVariableHere”) array/struct/whatever they call it. THIS is only for the basic text data.

//curl product function addproduct($request) { $url_to_submit = "somesite.com/addProducts.asp"; $header = array(); $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; $header[] = "Cache-Control: max-age=0"; $header[] = "Connection: keep-alive"; $header[] = "Keep-Alive: 300"; $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; $header[] = "Accept-Language: en-us,en;q=0.5"; $header[] = "Pragma: "; $header[] = "REMOTE_ADDR: ".$_SERVER['REMOTE_ADDR']; $header[] = "HTTP_X_FORWARDED_FOR: ".$_SERVER['REMOTE_ADDR']; //call postVars to format $_POST data $post = postVars($request); $curl_connection = curl_init($url_to_submit); curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($curl_connection, CURLOPT_REFERER, 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']); curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data")); curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl_connection, CURLOPT_POST, 1); curl_setopt($curl_connection, CURLOPT_COOKIEJAR, "cookie.txt"); curl_setopt($curl_connection, CURLOPT_COOKIEFILE, "cookie.txt"); curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post); curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $header); $data = curl_exec($curl_connection); curl_close($curl_connection); return $data; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 //curl product function addproduct ( $request ) { $url_to_submit = & quot ; somesite . com / addProducts . asp & quot ; ; $header = array ( ) ; $header [ 0 ] = & quot ; Accept : text / xml , application / xml , application / xhtml + xml , & quot ; ; $header [ 0 ] . = & quot ; text / html ; q = 0.9 , text / plain ; q = 0.8 , image / png , * / * ; q = 0.5 & quot ; ; $header [ ] = & quot ; Cache - Control : max - age = 0 & quot ; ; $header [ ] = & quot ; Connection : keep - alive & quot ; ; $header [ ] = & quot ; Keep - Alive : 300 & quot ; ; $header [ ] = & quot ; Accept - Charset : ISO - 8859 - 1 , utf - 8 ; q = 0.7 , * ; q = 0.7 & quot ; ; $header [ ] = & quot ; Accept - Language : en - us , en ; q = 0.5 & quot ; ; $header [ ] = & quot ; Pragma : & quot ; ; $header [ ] = & quot ; REMOTE_ADDR : & quot ; . $_SERVER [ 'REMOTE_ADDR' ] ; $header [ ] = & quot ; HTTP_X_FORWARDED_FOR : & quot ; . $_SERVER [ 'REMOTE_ADDR' ] ; //call postVars to format $_POST data $post = postVars ( $request ) ; $curl_connection = curl_init ( $url_to_submit ) ; curl_setopt ( $curl_connection , CURLOPT_CONNECTTIMEOUT , 30 ) ; curl_setopt ( $curl_connection , CURLOPT_USERAGENT , & quot ; Mozilla / 4.0 ( compatible ; MSIE 6.0 ; Windows NT 5.1 ) & quot ; ) ; curl_setopt ( $curl_connection , CURLOPT_REFERER , 'http://' . $_SERVER [ 'SERVER_NAME' ] . $_SERVER [ 'REQUEST_URI' ] ) ; curl_setopt ( $curl_connection , CURLOPT_HTTPHEADER , array ( & quot ; Content - type : multipart / form - data & quot ; ) ) ; curl_setopt ( $curl_connection , CURLOPT_RETURNTRANSFER , 1 ) ; curl_setopt ( $curl_connection , CURLOPT_SSL_VERIFYPEER , 0 ) ; curl_setopt ( $curl_connection , CURLOPT_FOLLOWLOCATION , 1 ) ; curl_setopt ( $curl_connection , CURLOPT_POST , 1 ) ; curl_setopt ( $curl_connection , CURLOPT_COOKIEJAR , & quot ; cookie . txt & quot ; ) ; curl_setopt ( $curl_connection , CURLOPT_COOKIEFILE , & quot ; cookie . txt & quot ; ) ; curl_setopt ( $curl_connection , CURLOPT_POSTFIELDS , $post ) ; curl_setopt ( $curl_connection , CURLOPT_HTTPHEADER , $header ) ; $data = curl_exec ( $curl_connection ) ; curl_close ( $curl_connection ) ; return $data ; }

Function 3: addphoto() an uploaded file

The data I had for the product photo was an existing URL, not the actual file. So I had to download the photo to a temporary location and create the $data/$_POST array myself instead of using the postVars function from above. Also the headers changed a little bit in dealing with this new type of form data.

//curl product photo function addphoto($request,$fileurl) { file_put_contents('photo.jpg', file_get_contents($fileurl)); $url_to_submit = "somesite.com/uploadPhoto.asp"; $header = array(); $header[] = "Cache-Control: max-age=0"; $header[] = "Connection: keep-alive"; $header[] = "Keep-Alive: 300"; $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; $header[] = "Accept-Language: en-us,en;q=0.5"; $header[] = "Pragma: "; $header[] = "REMOTE_ADDR: ".$_SERVER['REMOTE_ADDR']; $header[] = "HTTP_X_FORWARDED_FOR: ".$_SERVER['REMOTE_ADDR']; //manually created array for CURLOPT_POSTFIELDS $data = array( "file" => "@photo.jpg", "productid" => $request['productid'], "saveto" => "database" ); $curl_connection = curl_init($url_to_submit); curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($curl_connection, CURLOPT_REFERER, 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']); curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data")); curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl_connection, CURLOPT_POST, 1); curl_setopt($curl_connection, CURLOPT_COOKIEJAR, "cookie.txt"); curl_setopt($curl_connection, CURLOPT_COOKIEFILE, "cookie.txt"); curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $data); curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $header); $data = curl_exec($curl_connection); curl_close($curl_connection); return $data; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 //curl product photo function addphoto ( $request , $fileurl ) { file_put_contents ( 'photo.jpg' , file_get_contents ( $fileurl ) ) ; $url_to_submit = & quot ; somesite . com / uploadPhoto . asp & quot ; ; $header = array ( ) ; $header [ ] = & quot ; Cache - Control : max - age = 0 & quot ; ; $header [ ] = & quot ; Connection : keep - alive & quot ; ; $header [ ] = & quot ; Keep - Alive : 300 & quot ; ; $header [ ] = & quot ; Accept - Charset : ISO - 8859 - 1 , utf - 8 ; q = 0.7 , * ; q = 0.7 & quot ; ; $header [ ] = & quot ; Accept - Language : en - us , en ; q = 0.5 & quot ; ; $header [ ] = & quot ; Pragma : & quot ; ; $header [ ] = & quot ; REMOTE_ADDR : & quot ; . $_SERVER [ 'REMOTE_ADDR' ] ; $header [ ] = & quot ; HTTP_X_FORWARDED_FOR : & quot ; . $_SERVER [ 'REMOTE_ADDR' ] ; //manually created array for CURLOPT_POSTFIELDS $data = array ( & quot ; file & quot ; =& gt ; & quot ; @ photo . jpg & quot ; , & quot ; productid & quot ; =& gt ; $request [ 'productid' ] , & quot ; saveto & quot ; =& gt ; & quot ; database & quot ; ) ; $curl_connection = curl_init ( $url_to_submit ) ; curl_setopt ( $curl_connection , CURLOPT_CONNECTTIMEOUT , 30 ) ; curl_setopt ( $curl_connection , CURLOPT_USERAGENT , & quot ; Mozilla / 4.0 ( compatible ; MSIE 6.0 ; Windows NT 5.1 ) & quot ; ) ; curl_setopt ( $curl_connection , CURLOPT_REFERER , 'http://' . $_SERVER [ 'SERVER_NAME' ] . $_SERVER [ 'REQUEST_URI' ] ) ; curl_setopt ( $curl_connection , CURLOPT_HTTPHEADER , array ( & quot ; Content - type : multipart / form - data & quot ; ) ) ; curl_setopt ( $curl_connection , CURLOPT_RETURNTRANSFER , 1 ) ; curl_setopt ( $curl_connection , CURLOPT_SSL_VERIFYPEER , 0 ) ; curl_setopt ( $curl_connection , CURLOPT_FOLLOWLOCATION , 1 ) ; curl_setopt ( $curl_connection , CURLOPT_POST , 1 ) ; curl_setopt ( $curl_connection , CURLOPT_COOKIEJAR , & quot ; cookie . txt & quot ; ) ; curl_setopt ( $curl_connection , CURLOPT_COOKIEFILE , & quot ; cookie . txt & quot ; ) ; curl_setopt ( $curl_connection , CURLOPT_POSTFIELDS , $data ) ; curl_setopt ( $curl_connection , CURLOPT_HTTPHEADER , $header ) ; $data = curl_exec ( $curl_connection ) ; curl_close ( $curl_connection ) ; return $data ; }

Of course all the returned $data’s were just the html contents of the script’s Success Page so it was easy to just print it to the screen and make sure all was well. This is what worked for me and hopefully it will help someone else!