1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need php advice

  1. May 13, 2007 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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 (Text):

    #!/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: May 13, 2007
  2. jcsd
  3. May 13, 2007 #2

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Here is something I tried in the php, just to try to get some content from Amazon back, but it didn't work:
    Code (Text):

    #!/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;
    ?>
     
     
  4. May 13, 2007 #3

    -Job-

    User Avatar
    Science Advisor

    You're missing a ";" on your php script in the following line:
    Code (Text):

    $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]
     
    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 (Text):

    <?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;
    ?>
     
     
  5. May 13, 2007 #4

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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 (Text):

    <?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;
    ?>
     
     
  6. May 13, 2007 #5

    -Job-

    User Avatar
    Science Advisor

    Did you include the following line at the top of the file?
    Code (Text):

    #!/usr/local/bin/php
     
     
  7. May 13, 2007 #6

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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!
     
  8. May 13, 2007 #7

    -Job-

    User Avatar
    Science Advisor

    Glad to help.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Need php advice
  1. Beginner needs advice (Replies: 2)

Loading...