Forum Moderators: coopster

Message Too Old, No Replies

Using php for a repetitive task

         

mkingsle

6:58 pm on Jan 7, 2008 (gmt 0)

10+ Year Member



Hello, Newby Here:

I have a question about a task that I would like to simplify. I am relatively new to php and just learning the basics right now. Basically what I have is a website that sells Amazon products. I am using a piece of xml code that I found on the Amazon Web Services website in order to pull data from Amazon (just the price in my situation). The code is the following:

<?php
//enter your AWS ID
define('KEYID','MY KEY GOES HERE');
//enter your Amazon Associates ID
define('AssocTag','MY ASSOCIATE TAG GOES HERE');
//enter any ASIN you would like
$asin="WHATEVER THE ASIN # IS";
//define type of ASIN to better display it must be Books, Music, or DVD - you could define others starting at line 63
$SearchIndex="HomeGarden";
?>

<?php
$request = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService& AWSAccessKeyId=".KEYID."&AssociateTag=".AssocTag."&Version=2007-02-22& Operation=ItemLookup&ItemId=$asin&ResponseGroup=Medium,Offers";

$session = curl_init($request);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
//$response = file_get_contents($request);
$parsed_xml = simplexml_load_string($response);
print("<span class=\"price\">".$parsed_xml->Items->Item->OfferSummary-> LowestNewPrice->FormattedPrice."</span>");
?>

Initially on my website, I put this code on each individual page with the respective ASIN # pertaining to the product. I've recently began updating my website and removing that code and putting it into a new file that serves as an include. So each page now has an include file in the spot I need it. Each page is currently running a separate include file. It's taking me forever to do.

My question is this: Is it possible to automate this a little better so I don't have to do thousands of different include files, one for each sku # that I have? Could I possibly do one file with the code above, and then another file that is an array or something that associates all my sku's to their respective ASIN. Then on each page put a reference of it to go to the files and process the information and then output the result?

I'm sure getting confused thinking about it. Any help or guidance is greatly appreciated.

Michael

[edited by: dreamcatcher at 9:46 am (utc) on Jan. 8, 2008]
[edit reason] Fixed side scroll. [/edit]

PHP_Chimp

8:40 pm on Jan 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have no experience with dealing with Amazon from the resellers prospective. So im not sure how sku's and ASIN work together. You may well be able to put them together in an array.
So I cant directly help with that...however...

On the include page have you put that code into a function?
As if you have then you could call that function with the sku you want.
An example -


function not_very_useful () {
print 'Im not very useful, as this is pre-set';
}
function more_useful ($sku) {
print "This is your sku: $sku\n";
}

The more_useful function will print the sku that you pass to it when you call it. So you call more_useful('sku_2') and the output will be -
This is your sku: sku_2

So you put your function in the include page then call the function with the correct sku on each of your product pages.

If you want to get even more automated then you could use $_GET to pass the variable. Something like more_useful($_GET['sku']) if the url is example.com/products?sku=something
You could then look at a page template for each of your products. So you only have a single page that is populated by the results of your search.

You may want to get the first idea up and running, then look at building a template so you dont have to manually rebuild each of your pages next time. You said you are new to php, so I kept the examples simple...maybe too simple, if so I apologies.
If my example doesnt give you ideas, or you already have something like that and it isnt working then if you post the contents of the include page so we can see where you are coming from.

adwatson

10:00 pm on Jan 7, 2008 (gmt 0)

10+ Year Member



Perhaps you could get by with just one include by determining the calling page's name to determine the sku? Without knowing the exact structure of your site, it's hard to tell - but it seems that if you have one page per item, then you should be able to use that page's URL to determine you product number, perhaps through a database lookup table or something.

mkingsle

3:57 am on Jan 8, 2008 (gmt 0)

10+ Year Member



Hi Again:

Just thought I would clarify.

What I have for the include page is the following code:

1. <?php
2. define('KEYID','MY KEY GOES HERE');
3. define('AssocTag','MY ASSOCIATE TAG GOES HERE');
4. $asin="WHATEVER THE ASIN # IS";
5. $SearchIndex="HomeGarden";
6.?>

8. <?php
9. $request =
10."http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService& AWSAccessKeyId=".KEYID."&AssociateTag=".AssocTag."&Version=2007-02-22& Operation=ItemLookup&ItemId=$asin&ResponseGroup=Medium,Offers";
11. $session = curl_init($request);
12. curl_setopt($session, CURLOPT_HEADER, false);
13. curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
14. $response = curl_exec($session);
15. curl_close($session);
16. //$response = file_get_contents($request);
17. $parsed_xml = simplexml_load_string($response);
18. print("<span 19.class=\"price\">".$parsed_xml->Items->Item->OfferSummary-> LowestNewPrice->FormattedPrice."</span>");
20.?>

I put in line numbers so I can reference it.

The code is an Amazon code to find the updated price for me based on the ASIN they use. I won't go into detail for that because that is not my issue.

The way my site is setUp up s that each product page has it's own sku number, not in a database using SQL, just sku's I made up that I keep in excel.

I'll try to illustrate my situation a little better by giving an example.

Let's say I have a product page called http://www.example.com/brandname/prod1.php
Now the sku I am using in this example is prod1, which let's say has an Amazon ASIN # of 12345678B.

On that product page, I have a spot where I want to give the price. What I am starting to do right now is creating separate include files for each individual page. If you look at line 3 of the code above, which is the include file, this is where I put in the ASIN #, In this example it would be the 12345678B. Then on the product page, I just call it with the include statement. Easy enough if I have 10 products or so. But I have 1,000 to do. Doesn't make sense to do it this way.

So what I was thinking was, there must be a way of:

1.) Creating a file that I can put all my sku's into and make the relationship to the asin.

2.) Just have one include file with the code up top.

3.) Put an include link on each product page that will have the sku, which go's the include file and automatically populates the asin # based on the sku, and puts that asin # on the line 3.

Like I said before, I am really new to this, and probably shouldn't be jumping ahead like this because it seems as though this might be more advanced stuff. But I thought I would throw it out there anyway. I am just beginnin to read about arrays and functions, and it seems as though there might be a solution utilizing that. I just can't put the puzzle together yet.

Once again, any help is really appreciated.

Thanks,

Michael

[edited by: dreamcatcher at 9:47 am (utc) on Jan. 8, 2008]
[edit reason] Fixed side scroll. [/edit]

PHP_Chimp

7:54 pm on Jan 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




// This to go in the include page.
// So that you can re-use this code on all of your other pages.
//
define('KEYID','MY KEY GOES HERE');
define('AssocTag','MY ASSOCIATE TAG GOES HERE');
// function imaginatively called get_info...although you can change that
function get_info ($sku) {
$asin= $sku; //"WHATEVER THE ASIN # IS";
$SearchIndex="HomeGarden";
$request ="http://ecs.amazonaws.com/onca/xml? Service=AWSECommerceService&AWSAccessKeyId=".KEYID. "&AssociateTag=".AssocTag."&Version=2007-02-22& Operation=ItemLookup&ItemId=$asin&ResponseGroup=Medium, Offers";
$session = curl_init($request);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
//$response = file_get_contents($request);
$parsed_xml = simplexml_load_string($response);
$output = ("<span class=\"price\">".$parsed_xml->Items->Item->OfferSummary-> LowestNewPrice->FormattedPrice."</span>");
// NEW LINE
return $output;
}

As I said before Im not exactly sure how the sku's and asin's work together. So this function may need some work to actually get it going.
With the function as above you would use it like this -
echo get_info('your_sku');
As the echo will print out the return value of the function. So in this case the return value was the original print statement.

Now you may want to change a few things with this function.
At the moment the $sku is the same as the $asin...this may not be correct.
Also you have $SearchIndex="HomeGarden"; fixed. If you wanted to change the $SearchIndex then you could change the first line of the function to -
function get_info ($sku, $SearchIndex) {
If you usually want HomeGarden as the search index, but sometimes may want something different then you could specify that as a default option.
function get_info ($sku, $SearchIndex = 'HomeGarden') {
Then you can call the function as either -


get_info(12323); // assuming sku = 12323 using the default $SearchIndex
// or
get_info(12323, 'SomeThingElse'); // same sku but this time $SearchIndex = 'SomeThingElse';

Hope that gives you some ideas.

<edit>
Just read your post again. So I think I have a better idea about sku's -> asins.
Assuming that your page names are always along the lines of -
example.com/brandName/prod_1


$url_bits = explode('/', $_SERVER['PHP_SELF']);
$last_bit = count($url_bits);
$prod = $url_bits[$last_bit-1]; // prod_1 in this example
echo get_info($prod);

On the include page -

$skus = array('prod_1' => '12345678B', 'prod_2' => '12345679B');
function get_info ($sku) { // get_info ('prod_1') {
global $skus;
$asin = $skus[$sku]; // $asin = $skus['prod_1'] => '12345678B'
...

[edited by: PHP_Chimp at 8:04 pm (utc) on Jan. 8, 2008]

[edited by: jatar_k at 2:18 pm (utc) on Jan. 15, 2008]
[edit reason] fixed sidescroll [/edit]

mkingsle

3:59 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



Hi again:

I really appreciate all the hard work that you have put into this. It's been really helpful and appreciated.

I do have a few questions and hangups still outstanding with this.

I think I have a good understanding of what is to be put into the include page. Although further explanation might be needed.

My hangup is with what to do on the product page. As it stands right now, I have the following code to call up the individual include file that runs the code for the price:

<p class="pricing">1 lb. Price:<?php include ("../prices/product_1.php");?></p>

What I am now confused with is that with the new include file that has been provided, I have the array in there with the multiple sku's => asin's:

$skus = array(
'prod_1' => '12345678A',
'prod_2' => '12345678B');
function get_info ($sku) { // get_info ('prod_1') {
global $skus;
$asin = $skus[$sku]; // $asin = $skus['prod_1'] => '12345678B'

I'm still confused on what to put on the product page and how it will know to go to the include file, find the ASIN based on my sku, process through the code, return price and print it on my product page.

Thanks in advance.

Michael