<?php /* Con-way XML Sample Code for PHP 5 - Image Retrieval interface This code has been tested on PHP 5.04 using the SimpleXML Extension ----------------- TODO: * replace the USERNAME and PASSWORD String values below with your Con-way username and password * replace PRONUMBER strings below with your own 9-digit PRO Numbers * update $imageDir with the location to which you want to write * the image files on your webserver * update $imageUrl with the URL to access the files NOTE: you must have the cURL libraries installed with PHP on your server-- If you need them, see your System Administrator, who can get then at http://curl.haxx.se/download.html ----------------- Send questions to Con-way XML Support at xmlsupport@Con-way.com */ $title = "Image Retrieval"; $requestType = "ImageRequest"; $conwayUrl = "https://www.Con-way.com/"; $servletUri = "webapp/imaging_app/XmlImageRetrieverServlet"; $requestUrl = $conwayUrl.$servletUri; // TODO update $imageDir with the location to which you want to write // the image files on your webserver // update $imageUrl with the URL to access the files $imageDir = "C:\\Program Files\\Apache Group\\Apache2\\htdocs\\conwayImages\\"; $imageUrl = "http://localhost/conwayImages/"; // replace the USERNAME and PASSWORD String values below with your Con-way username and password $username = "USERNAME"; $password = "PASSWORD"; $myInput = $_GET; // array of input data // If you don't pass data in as a GET variables, hard code them here: // TODO: replace PRONUMBER string below with your own 9-digit PRO Number if (! isset($myInput['pro'])) { $myInput['pro'] = "PRONUMBER"; } if (! isset($myInput['type'])) { $myInput['type'] = "BL"; // Type may be BL, DR, LOA, WI } if (! isset($myInput['format'])) { $myInput['format'] = "jpeg"; // Format may be pdf, jpeg, png, tiff } // array of elements you want to query from the XML Response $myElements = array('PRONumber', 'ImageType', 'ImageFormat'); /* Build XML Request In actual use, you would probably populate the XML Request parameters from data submitted via an on-line order form or database. For this sample we will just hard code some dummy data. */ extract($myInput); $type = strtoupper($type); $format = strtoupper($format); $xmlRequest="<$requestType>" . "<ImageKey>" . "<PRONumber>$pro</PRONumber>" . "<ImageType>$type</ImageType>" . "<ImageFormat>$format</ImageFormat>" . "</ImageKey>" . $xmlRequest .= "</$requestType>"; //Convert characters to proper format for HTTP POST $xmlRequest = urlencode($xmlRequest); // open synchronous connection to Con-way servlet and set options $urlConn = curl_init ($requestUrl); curl_setopt ($urlConn, CURLOPT_POST, 1); curl_setopt ($urlConn, CURLOPT_SSL_VERIFYPEER, false); // May be needed for SSL behind a firewall curl_setopt ($urlConn, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded")); curl_setopt ($urlConn, CURLOPT_USERPWD, $username.":".$password); curl_setopt ($urlConn, CURLOPT_POSTFIELDS, "$requestType=$xmlRequest"); // Parse the XML Response ob_start(); // prevent the buffer from being displayed curl_exec($urlConn); $xmlResponse = simplexml_load_string( ob_get_contents() ); ob_end_clean(); curl_close($urlConn); // close the connection // Build the HTML page echo " <html> <head> <title>Sample PHP 5 for Con-way XML $title></title> <script language=\"Javascript\" type=\"text/javascript\"> <!-- function erase(object) { object.value=\"\"; } // --> </script> </head> <body text=\"SlateBlue\"> <center> <B><font face=\"arial\" size=+1><I>Sample PHP 5 Con-way XML $title</I></font></B> </center> <br> <form action=\"". $_SERVER['PHP_SELF'] ."\" method=\"get\"> <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> "; foreach ($myInput as $key => $value) { if ($key != "Submit") echo " <tr> <td><b>" . strtoupper($key) . ":&nbsp;&nbsp;</b></td> <td><input type=\"text\" name=\"$key\" value=\"$value\" onFocus=\"erase(this);\" onClick=\"erase(this);\"></td> </tr> "; } echo " <tr><td>&nbsp;</td><td>&nbsp;</td></tr> <tr> <td>&nbsp;</td> <td><input type=\"submit\" name=\"Submit\" value=\"Process $title\"></td> </tr> </table> </form> <hr> "; // Extract your element values from the XML Response // The outer loop iterates the total number of occurrences of your first element for ($i=0; $i < count($xmlResponse->xpath('//'.$myElements[0])); $i++) { // The inner loop iterates all of your elements, for each occurrence of the first foreach ($myElements as $myEl) { $myVals = $xmlResponse->xpath('//'.$myEl); echo "<b>Value of &lt;$myEl&gt; is: $myVals[$i]</b><BR>";; } echo '<br>'; } // Get array of encoded image data $imageData = $xmlResponse->xpath('//'."ImageData"); $pages = count($imageData); // number of image pages // If image retrieval failed, display XML response with error if ($pages==null) { echo "<b>Here is the dump of the XML output for $title:</b><br>" . "<code>$xmlResponse</code><br>"; } echo "<b>Total image pages: $pages</b><hr>"; $uniqueString = date("YmdHis"); // unique number from current time for ($i=0; $i < $pages; $i++) { $imageDecoded = base64_decode($imageData[$i]); // decode binary image data $pageString = ""; // page number for filename if ($pages > 1) { $pageString = $i+1; // if multiple pages, set page number and format if (strlen($pageString) < 2) $pageString = "0".$pageString; $pageString = "_".$pageString; } $imageLabel = "$type image for PRO number $pro - $pageString"; $fileName = $type.'-'.$pro.'-'.$uniqueString.$pageString.'.'.$format; file_put_contents($imageDir.$fileName, $imageDecoded); $fileUrl = $imageUrl.$fileName; echo "<img src=\"$fileUrl\" border=\"0\" alt=\"$imageLabel\" title =\"$imageLabel\">"; } echo "</body>" . "</html>"; ?>