PHP as CGI

General ShopSite user discussion

PHP as CGI

Postby tbirnseth » Wed Aug 01, 2007 3:48 pm

I want to utilize PHP for cgi. I.e. I want to write in php (no output to screen or user) to perform various functions.

How does ShopSite call a cgi-script. I.e is it done directly through the web-server as a POST (if so, I should just be able to use any php file with a .php extension). Or does it do it through a shell interpeter (in this case, seems I would need an interpeter header like '#!/usr/local/bin/php -q'.

I try to account for POSTed info to come either on stdin or via the PHP $_POST variable (precedence given to stdin).

Can anyone advise? I've read the posts that state you can't use .php in cgi-bin. But if it's interpeted b the the shell or the kernel, it should acknowlege the magic_number (from my old Unix days) of '#!/usr/local/bin/php'.

Help please.

script I'm trying as an experiment is below. But don't want to break a customer's site if it would blow up.

[code]
#!/usr/local/bin/php -q
<?php
echo "Content-type: text/html\n\n";
$sendMail = TRUE;
$usePost = TRUE;
$useStdIn = TRUE;

function preOut($str) { echo "$str\n"; }

$postParams = array();
$subject = 'Order get/post for ShopSite';
$to = 'tonyb@1sit.com';
$from = 'noone@example.com';
$cc = '';
$headers = "From: $from\r\n".($cc ? "Cc: $cc\r\n" : "");
$body = '';

$gotFrom = 'No One';
if( $useStdIn ) {
$stdin = @fopen("php://stdin", "r");
$postString = @fread($stdin, $_SERVER['CONTENT_LENGTH']);
@parse_str($postString, $postParams);
if( $postParams )
$gotFrom = 'Stdin';
}
// If there was nothing on stdin, then see if $_POST is set, has elements and that we didn't end up with postParms set
// from using stdin.
if( $usePost && isset($_POST) && count($_POST) && !$postParams) {
$postParams = $_POST;
if( $postParams )
$gotFrom = '_POST';
}


$body .= "\nPost Parameters from $gotFrom:\n";
foreach($postParams as $name => $value)
$body .= sprintf("\t%20s: %s\n", $name, $value);

if( $sendMail )
mail($to, $subject, $body, $headers);
?>
[/code]
tbirnseth
 
Posts: 1
Joined: Wed Aug 01, 2007 3:38 pm
Location: Portland Oregon

Postby loren_d_c » Thu Aug 02, 2007 9:18 am

You can use PHP scripts for the Order API and Custom Shipping Add-on api in the shopping cart, both ShopSite Pro features, search for them in the ShopSite documentation to see what parameters are available. See the following threads from this very forum regarding using PHP for these api's:

http://support.shopsite.com/forums/viewtopic.php?t=2028
http://support.shopsite.com/forums/viewtopic.php?t=2173
http://support.shopsite.com/forums/viewtopic.php?t=2105
http://support.shopsite.com/forums/viewtopic.php?t=2499

-Loren
loren_d_c
 
Posts: 2572
Joined: Fri Aug 04, 2006 12:02 pm
Location: Anywhere


Return to User Forum

Who is online

Users browsing this forum: No registered users and 56 guests