How to use PHP for Custom Shipping

General ShopSite user discussion

How to use PHP for Custom Shipping

Postby Michael Wong » Tue Dec 20, 2005 11:14 am

OK, I've discovered how to use PHP to make Custom Shipping CGI scripts,
in case not everyone wants to use PERL. Unlike normal PHP scripts, this
one is run from the command-line (that was the trick that first eluded
me about the Custom Shipping module), so you need to have the shell
interpreter line at the beginning of the script. You also need to have
PHP-CLI installed (the PHP command-line interface), and you need to have
the PHP executable in the /usr/local/bin directory (or you have to alter
the following code to set the correct directory in the first line).

The following example script can easily be converted to a debugging
script by altering the if/then conditional so that it always performs
the "else" code, in which case it will dump all of the input parameters
to the error message which shows up in Shopsite (very handy when your
shipping module is behaving strangely and you're not sure why).

I'm not posting this to ask for help because I've already figured it out
on my own; I just thought I'd share because the official documentation
assumes that everyone wants to use PERL, and I've grown accustomed to
PHP. So without further ado, here's an example of a PHP-scripted Custom
Shipping module (I removed the actual rate calculations themselves in
order to save space, but they vary from installation to installation
anyway so they would be of little use to anyone but me):



#!/usr/local/bin/php -q
<?PHP

// Note: $_POST variable not available since this script is called as a
// CGI script, so we have to read in our params from stdin, with length
// determined by the CONTENT_LENGTH environment variable.

// Read in POST parameters sent by ShopSite
$stdin = fopen("php://stdin","r");
fscanf($stdin,"%{$SERVER['CONTENT_LENGTH']}s",$query_string);

// And now we split the params into the $params array.
parse_str($query_string,$params);

if ( $params['cust_country'] && $params['item_total'] )
{
// You would normally include code here for determining
// your shipping rates and options. Remember that all input
// parameters are in the $params array.
//
// The variables to set for the output (assuming you use the
// output code included in this example) are $status, $options,
// and $prices. $options and $prices are arrays.
}
else
{
$status="fail";
$error=urlencode("Params are ".var_export($params,true));
$option_count=0;
}

// Output data to ShopSite
echo "status=".$status."\n";
echo "option_count=".$option_count."\n";
if ($status="fail")
{
echo "error=".$error."\n";
}
for ( $option = 1; $option <= $option_count; $option += 1 )
{
echo "s".$option."option=".$options["$option"]."\n";
echo "s".$option."price=".$prices["$option"]."\n";
}

?>
Michael Wong
 

Re: How to use PHP for Custom Shipping

Postby Scott Norman » Mon Jan 09, 2006 9:34 pm

Hi Micheal,

I'm glad you posted this, I've wanted to use PHP for order api and for
shipping api (may have one to do soon), but couldn't get it too work, so
I just left it, didn't have time to dig into. This is great! I know PHP
better then Perl.. Now just have to bug the techs to make sure PHP
command line is setup. Thanks for posting and taking the time to figure
it out.

Thanks,

Scott Norman
YourHost.com

Michael Wong wrote:
OK, I've discovered how to use PHP to make Custom Shipping CGI scripts,
in case not everyone wants to use PERL. Unlike normal PHP scripts, this
one is run from the command-line (that was the trick that first eluded
me about the Custom Shipping module), so you need to have the shell
interpreter line at the beginning of the script. You also need to have
PHP-CLI installed (the PHP command-line interface), and you need to have
the PHP executable in the /usr/local/bin directory (or you have to alter
the following code to set the correct directory in the first line).

The following example script can easily be converted to a debugging
script by altering the if/then conditional so that it always performs
the "else" code, in which case it will dump all of the input parameters
to the error message which shows up in Shopsite (very handy when your
shipping module is behaving strangely and you're not sure why).

I'm not posting this to ask for help because I've already figured it out
on my own; I just thought I'd share because the official documentation
assumes that everyone wants to use PERL, and I've grown accustomed to
PHP. So without further ado, here's an example of a PHP-scripted Custom
Shipping module (I removed the actual rate calculations themselves in
order to save space, but they vary from installation to installation
anyway so they would be of little use to anyone but me):



#!/usr/local/bin/php -q
?PHP

// Note: $_POST variable not available since this script is called as a
// CGI script, so we have to read in our params from stdin, with length
// determined by the CONTENT_LENGTH environment variable.

// Read in POST parameters sent by ShopSite
$stdin = fopen("php://stdin","r");
fscanf($stdin,"%{$SERVER['CONTENT_LENGTH']}s",$query_string);

// And now we split the params into the $params array.
parse_str($query_string,$params);

if ( $params['cust_country'] && $params['item_total'] )
{
// You would normally include code here for determining
// your shipping rates and options. Remember that all input
// parameters are in the $params array.
//
// The variables to set for the output (assuming you use the
// output code included in this example) are $status, $options,
// and $prices. $options and $prices are arrays.
}
else
{
$status="fail";
$error=urlencode("Params are ".var_export($params,true));
$option_count=0;
}

// Output data to ShopSite
echo "status=".$status."\n";
echo "option_count=".$option_count."\n";
if ($status="fail")
{
echo "error=".$error."\n";
}
for ( $option = 1; $option <= $option_count; $option += 1 )
{
echo "s".$option."option=".$options["$option"]."\n";
echo "s".$option."price=".$prices["$option"]."\n";
}

?
Scott Norman
 

Re: How to use PHP for Custom Shipping

Postby fillinthe_____ » Fri Jun 14, 2013 3:34 pm

If you are using shared hosting, you won't have permission to run the php script (unless you use an expensive virtual private server). You'll have to use a perl script instead. I just spent the last too many hours figuring this out, hopefully I can save you some time.

Caleb
fillinthe_____
 
Posts: 2
Joined: Thu Jun 13, 2013 3:29 pm

Re: How to use PHP for Custom Shipping

Postby IrongateE » Wed Dec 03, 2014 11:53 am

Thanks, Michael, for your very helpful PHP code. One issue I ran into was the script having trouble parsing anything after a space in the input. For example, if there was a space in the zip code (some international addresses have them) then the script would fail to return shipping options. I fixed this by replacing these lines:

Code: Select all
$stdin = fopen("php://stdin","r");
fscanf($stdin,"%{$SERVER['CONTENT_LENGTH']}s",$query_string);


With this:

Code: Select all
$in = fopen('php://stdin', 'r');
$query_string = '';
while(!feof($in)){
    $query_string .= fgets($in, 4096);
}


Hope that helps!

Glen
IrongateE
 
Posts: 4
Joined: Tue May 14, 2013 2:41 pm

Re: How to use PHP for Custom Shipping

Postby dgray » Wed Jan 07, 2015 9:22 am

loren_d_c posted the following one-liner in 2008 in the topic "Custom Shipping API - errorno 32?"

Code: Select all
$query_string = trim(fgets(STDIN));


which apparently solves the spurious spaces problem. That thread also includes a nice code example.

One other gotcha that caught me out, customers sometimes purchase unusually large numbers of different items, causing a php MAX_INPUT_VARS exceeded error message. The only way I could find to increase the max_input_vars in this mode of operating was to change the hashbang line at the start to:

Code: Select all
#!/usr/local/bin/php -d max_input_vars=3000


The price for this was I had to remove the -q (quiet) switch, since the hashbang format only allows one parameter. (FreeBSD
shell passes all arguments as one.) Trying to use both -q and -d gives a server configuration error. But apparently in PHP CLI, the quiet mode is no longer needed so it's all good.

~ David
dgray
 
Posts: 54
Joined: Fri Sep 08, 2006 9:06 am


Return to User Forum

Who is online

Users browsing this forum: No registered users and 87 guests