Need php advice

  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,527
28

Main Question or Discussion Point

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 lets 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:

Answers and Replies

  • #2
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,527
28
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="[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"[/URL]

$xml=file_get_contents($url);

echo $xml; 
?>
 
Last edited by a moderator:
  • #3
-Job-
Science Advisor
1,146
1
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="[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="[/URL] . $kwords;

$xml=file_get_contents($url);

echo $xml; 
?>
 
Last edited by a moderator:
  • #4
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,527
28
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="[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="[/URL] . $kwords;

$xml=file_get_contents($url);

echo $xml; 
?>
 
Last edited by a moderator:
  • #5
-Job-
Science Advisor
1,146
1
Did you include the following line at the top of the file?
Code:
#!/usr/local/bin/php
 
  • #6
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,527
28
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!
 
  • #7
-Job-
Science Advisor
1,146
1
Glad to help.
 

Related Threads on Need php advice

Replies
7
Views
5K
  • Last Post
Replies
4
Views
636
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
8
Views
6K
  • Last Post
Replies
8
Views
4K
  • Last Post
Replies
12
Views
838
  • Last Post
2
Replies
40
Views
19K
  • Last Post
Replies
22
Views
4K
  • Last Post
Replies
9
Views
1K
  • Last Post
Replies
4
Views
5K
Top