PHP Help Needed: Combining PHP & Amazon Web Services for Midterm Project

AI Thread Summary
The discussion revolves around a midterm project involving PHP and Amazon's web services. The user is attempting to create a form that allows input of a keyword to search for books on Amazon. The initial PHP script was not functioning correctly because it lacked proper concatenation of the search keyword to the Amazon URL. A suggestion was made to use the `isset()` function to check for the keyword and concatenate it to the URL using the dot operator. The user implemented these changes in a revised PHP script, which successfully appended the keyword and retrieved data from Amazon. The user also acknowledged a missed line in the script that specifies the PHP interpreter, which was later added. Overall, the conversation highlights troubleshooting steps and coding corrections to achieve the desired functionality.
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,650
Reaction score
39
I am working on my midterm project and I have two pieces I can't put together with php.
I am supposed to create a form that let's a user enter a keyword to search Amazon books, so I have the form that catches the data and gives it to the php file:
http://www.pic.ucla.edu/~jctaylor/midterm.htm
the php is just this:
Code:
#!/usr/local/bin/php
<?php 
echo $_GET["hello"]; 
?>
"hello" is the variable that hold the entered search keyword.

I need the php script to append that keyword to a long URL that goes to Amazon web services and gets the books, and then returns the content to the div in my HTML.
I have a page with a link that will go get the books. I hard coded the search word "Oprah" in the query. (note: this only works correctly in firefox, as far as I know).
http://www.pic.ucla.edu/~jctaylor/getbooks.htm

Anyway..
I'm not sure how to make php append the keyword, get the data, and send it back.

Any tips? I am pretty lost. Thanks.
 
Last edited by a moderator:
Technology news on Phys.org
Here is something I tried in the php, just to try to get some content from Amazon back, but it didn't work:
Code:
#!/usr/local/bin/php
<?php 
$url="http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=06XN0NB7DG5T94WYZCR2&Operation=ItemSearch&ResponseGroup=Medium,Request&SearchIndex=Books&BrowseNode=1000&Sort=salesrank&Style=http://www.pic.ucla.edu/~jctaylor/midterm.xsl&Keywords=Oprah"

$xml=file_get_contents($url);

echo $xml; 
?>
 
Last edited by a moderator:
You're missing a ";" on your php script in the following line:
Code:
$url="[URL]http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=06XN0NB7DG5T94WYZCR2&Operation=ItemSearch&ResponseGroup=Medium,Request&SearchIndex=Books&BrowseNode=1000&Sort=salesrank&Style=http://www.pic.ucla.edu/~jctaylor/midterm.xsl&Keywords=Oprah"[B];[/B][/URL]

You want your PHP script to retrieve the "hello" variable from the querystring and append it to the URL. You use isset($_GET["hello"]) to check if the "hello" variable has a value.
To append the value to your amazon URL you concatenate the value to the $url variable using the "." (the concatenation operator):
Code:
<?php 
$kwords = "";
if(isset($_GET["hello"])) $kwords = $_GET["hello"];
$url="http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=06XN0NB7DG5T94WYZCR2&Operation=ItemSearch&ResponseGroup=Medium,Request&SearchIndex=Books&BrowseNode=1000&Sort=salesrank&Style=http://www.pic.ucla.edu/~jctaylor/midterm.xsl&Keywords=" . $kwords;

$xml=file_get_contents($url);

echo $xml; 
?>
 
Last edited by a moderator:
Thanks so much, Job!
Is there anything else I am missing? I am still having some trouble with it.

I made a new version here:
http://www.pic.ucla.edu/~jctaylor/midterm1.htm

and made changes to midterm1.php:
Code:
<?php 
$kwords = "";
if(isset($_GET["hello"])) $kwords = $_GET["hello"];
$url="http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=06XN0NB7DG5T94WYZCR2&Operation=ItemSearch&ResponseGroup=Medium,Request&SearchIndex=Books&BrowseNode=1000&Sort=salesrank&Style=http://www.pic.ucla.edu/~jctaylor/midterm.xsl&Keywords=" . $kwords;

$xml=file_get_contents($url);

echo $xml; 
?>
 
Last edited by a moderator:
Did you include the following line at the top of the file?
Code:
#!/usr/local/bin/php
 
Oh God! I'm an idiot! I had not put it in there. I am about blind from a severe headache today.

Job, I love you. You've saved me! I owe you big time!
 
Glad to help.
 
Back
Top