Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Sending an HTTP POST with some XML ?

Featured Replies

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 by Guest

  • 2 weeks later...

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).

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/

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.