cincin Posted January 23, 2010 Posted January 23, 2010 (edited) Hello all, I'm trying to adapt a php osCommerce shipping module to FMP so that I can provide shipping estimates from within a FMP DB. The thing is, I don't know php or xml, I also haven't really had the chance to play with the PHP, XML or even web viewer part of FMP, so I need some help. The php looks rather simple, it forms some XML using the data provided, uses an HTTP POST to send it to the Canada Post server and then parses the resulting XML back from Canada Post. I'm pretty sure I can accurately recreate the XML with my data in FMP. The part I'm not sure about is how to send it and get the reply. The php goes like this: /* using HTTP/POST send message to canada post server */ function _sendToHost($host,$port,$method,$path,$data,$useragent=0) { // Supply a default method of GET if the one passed was empty if (empty($method)) $method = 'GET'; $method = strtoupper($method); if ($method == 'GET') $path .= '?' . $data; $buf = ""; // try to connect to Canada Post server, for 2 second $fp = @fsockopen($host, $port, $errno, $errstr, 15); if ( $fp ) { fputs($fp, "$method $path HTTP/1.1n"); fputs($fp, "Host: $hostn"); fputs($fp, "Content-type: application/x-www-form-urlencodedn"); fputs($fp, "Content-length: " . strlen($data) . "n"); if ($useragent) fputs($fp, "User-Agent: Oscommercen"); fputs($fp, "Connection: closenn"); if ($method == 'POST') fputs($fp, $data); while (!feof($fp)) $buf .= fgets($fp,128); fclose($fp); } else { $buf = '<?xml version="1.0" ?>Cannot reach Canada Post Server. You may refresh this page (Press F5 on your keyboard) to try again.'; } return $buf; } function _canadapostGetQuote() { $strXML = "<?xml version="1.0" ?>"; // set package configuration. $strXML .= "n"; $strXML .= " " . $this->language . "n"; $strXML .= " n"; $strXML .= " " . $this->CPCID . "n"; $strXML .= " " . $this->_canadapostOriginPostalCode . "n"; $strXML .= " " . $this->turnaround_time . "n"; $strXML .= " " . (string)$this->items_price . "n"; // add items information. $strXML .= " n"; for ($i=0; $i < $this->items_qty; $i++) { $this->item_description[$i] = str_replace("&", "and", $this->item_description[$i]); $strXML .= " n"; $strXML .= " " . $this->item_quantity[$i] . "n"; $strXML .= " " . ($this->item_weight[$i]) . "n"; $strXML .= " " . $this->item_length[$i] . "n"; $strXML .= " " . $this->item_width[$i] . "n"; $strXML .= " " . $this->item_height[$i] . "n"; $strXML .= " " . $this->item_description[$i] . "n"; if ($this->item_readytoship[$i]) $strXML .= " n"; $strXML .= " n"; } $strXML .= " n"; // add destination information. $strXML .= " " . $this->dest_city . "n"; $strXML .= " " . $this->dest_province . "n"; $strXML .= " " . $this->dest_country . "n"; $strXML .= " " . $this->dest_zip . "n"; $strXML .= " n"; $strXML .= "n"; //print $strXML; if ($resultXML = $this->_sendToHost($this->server,$this->port,'POST','',$strXML)) { return $this->_parserResult($resultXML); } else { return false; } } Any hints ? Thank you Edited January 23, 2010 by Guest
Jason Lane Posted February 1, 2010 Posted February 1, 2010 Try putting a Web Viewer object on a layout. Specify a blank URL. Use the 'Object Info' panel to assign a name to this Web Viewer. Then write a FM script that uses 'Set Web Viewer [Go to url...]' and specify the URL (including the necessary key/value pairs). When you execute the FM script, it will send your data via GET to the script you posted, which will in turn (I guess) send a query via POST to the Canada Post system. You could get the reply by looking at the resulting content in the Web Viewer. Use the function GetLayoutAttribute to get the 'content' attribute of the Web Viewer object. Eg. 'GetLayoutAttribute ( "webViewerObjectName" ; "content" )' This will return the source code of what is being displayed in the Web Viewer. You can then parse that as necessary. The GetLayoutAttribute function will not evaluate if the page hasn't finished loading. So, you can use a loop that won't exit until it has managed to get the result. Something like this: Set Web Viewer [Objct Name: "webViewerObjectName"; URL: "http://localhost/osCommerce.php?" & $queryString] Loop Pause/Resume Script [Duration (seconds): .1] Set Variable [$response; Value:GetLayoutAttribute ( "webViewerObjectName" ; "content" ) Exit Loop If [not IsEmpty ( $response )] End Loop The above script will work, but it's quite basic and a real implementation should have some better error checking/handling (eg. you might want to have a timeout in case there's no reply from the server).
Jason Lane Posted February 2, 2010 Posted February 2, 2010 If you don't mind needing to use a plug-in, a better option might be one such as 360 Works' Scriptmaster, which can do both GET and POST. I hadn't tried it before, but I just tried it. It's free, and seems to work well. www.360works.com/scriptmaster/
Recommended Posts
This topic is 5406 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now