Click to See Complete Forum and Search --> : From Perl to PHP help - yahoo! store real-time inventory


Alex Merek
03-06-2002, 04:33 PM
Hello,

Would like to set up a way to keep the inventory in my yahoo! store current with the inventory our mysql tables. They offer a script in Perl that would do the job but my programming knowledge ends with basic php stuff.

I was hoping maybe someone could break this one down for me in basic terms.. or point me in the right direction with implementing the idea in PHP. I'd love to understand it rather than just be able to plug it in.

but any help would be much appreciated:

#!/usr/local/bin/perl

#
# Install (a modified version of) this program in your webserver's cgi-bin
# directory.
#
# This demo program handles a request to return inventory information for
# .catalog=&.id=_fake_yahoo_item_&.code=_fake_yahoo_code_
# It returns the item availability information as its response.
#

require 5.001;
use strict;

if ($ENV{'REQUEST_METHOD'} ne "POST") {
die("Expecting a POST, bailing");
}

my $o;
read(STDIN,$o,$ENV{'CONTENT_LENGTH'});

my %o;
for (split(/&/,$o)) {
$_ =~ s/\+/ /g;
my($key,$val) = split(/=/,$_,2);
for ($key,$val) {
$_ =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/ge;
}
$o{$key} = $val;
}

sub YahooFakeItem {
my($info)=@_;
my $itemid=$info->{".id"};


if (defined $itemid && $itemid eq "_fake_yahoo_item_") {
return 1;
} else {
return 0;
}
}

sub GetItemAvailability {
my($info)=@_;

my $catalog=$info->{".catalog"};
my $code=$info->{".code"};

#
# Replace this with your code to determine whether an item is in stock.
# Return -1, if the item is unknown
# available quantity, if the item is valid.
#
srand;
return int(rand(100));
}
my($avail);

if (YahooFakeItem(\%o)) {
$avail="-1";
} else {
$avail=GetItemAvailability(\%o);
}
print "Status: 200\n";
print "Content-Type: text/plain\n";
print "Available: $avail\n";


if ($avail <= 0) {
print "Inventory-Message: The item is not currently available. Please check back later.\n";
}

print "\n";


thanks,

Fandelem
03-08-2002, 04:05 AM
based on this:


# This demo program handles a request to return inventory information for
# .catalog=&.id=_fake_yahoo_item_&.code=_fake_yahoo_code_
# It returns the item availability information as its response.


you could have something like this:


<?php
$DB_CONF["hostname"] = "localhost";
$DB_CONF["username"] = "username_goes_here";
$DB_CONF["password"] = "database_password";
$DB_CONF["database"] = "database_name";
$DB_CONF["table_store1"] = "store1"; /* table store1 within database_name */

@mysql_pconnect($DB_CONF["hostname"],$DB_CONF["username"],$DB_CONF["password"])
or die ("<br>hmm.. server seems at full capacity, please click refresh in a few seconds." . mysql_error() );
$DB_CON = @mysql_select_db($DB_CONF["database"]) or die ("Couldn't select database." . mysql_error() );


/* for ****s and giggles, we are assuming that the HTML FORM looks like this:

echo "<form name=fake_yahoo_item method=post action=\"$PHP_SELF\">
catalog: <input type=text name=\"catalog\">
<p>
id: <input type=text name=\"id\">
<p>
code: <input type=text name=\"code\">
<p>
<input type=submit name=submit value=submit>
</form>";

when a user clicks on submit, the VALUES (ie. what is in each textbox)
are passed to $PHP_SELF (which is this script :), in the form of variables
that correspond with the name tag. For example, when you click on submit,
you gain a variable named, "catalog", "id", and "code".
*/

if ( $catalog && $id && $code )
{
/* you might want to do some error checking here for form values */

/* build our sql statement */
$sql = "SELECT category from {$DB_CONF["store1"]}
where catalog = '$catalog'
AND id = '$id'
AND code = '$code'";

/* query away */
$sql_result = mysql_query($sql) or die ("Couldn't execute query. (quotecategorybyquoteid()) ".mysql_error());

/* make sure query went through */
if ( !$sql_result )
{
echo "SQL result failed.";
exit;
}
/* find our availability */
$avail = 0;
$avail = mysql_num_rows($sql_result);

if ( $avail > 0 )
{
echo "Availability: $avail";
}
else
{
echo "There is no availablity.";
}

}
else /* default display */
{
echo "<html>\n\n<body><form name=fake_yahoo_item method=post action=\"$PHP_SELF\">
catalog: <input type=text name=\"catalog\">
<p>
id: <input type=text name=\"id\">
<p>
code: <input type=text name=\"code\">
<p>
<input type=submit name=submit value=submit>
</form></body></html>";
}


?>


NOTE: it is untested, and i just whipped it up real quick, but it should suffice, in fact, all you have to do is change the top variables to your needs (it should function as if you cut&paste that and go to it directly).

cheers,

kyle

Alex Merek
03-08-2002, 11:35 AM
Kyle,

thanks very much! That looks like more than enough to get me started :D

sincerely,