how would I code this in php?

Discussion in 'PHP' started by seofighter, Jul 16, 2010.

  1. #1
    Hi,,

    I am learning php for fun and have set myself a project to write a php script that when run does this:

    goes to amazon.co.uk
    searches for item you specified eg item number 123456
    'finds' the price on the page
    copies and pastes the current price to a txt file


    the 3rd part, where the code locates the price on the page is probably the trickiest - any suggestions how to go about this?
     
    seofighter, Jul 16, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    lol nice. Sounds hard, even for me, since I'm new to PHP too.

    I think it's possible with cURL. I made something that grabs the whole HTML page with it. Anyway, I'm sure there's better function than cURL.

    How far are you with PHP anyways? You should start with simple PHP+mySQL if you're a total beginner with programming.
     
    Last edited: Jul 17, 2010
    Rainulf, Jul 17, 2010 IP
  3. caciocode

    caciocode Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It is very hard and I would not recommend attempting it if you're an absolute beginner.

    You will need to sign up as an Amazon affiliate. Access their affiliate servers using REST/SOAP (web services) You can find the documentation here of the requests you can make. And that is not all; The requests have to be signed which is a very complex task, working with the returned data (XML) which requires to be good at problem solving, etc.

    The overall task is requires someone with a very advanced knowledge in PHP and XML.
     
    Last edited: Jul 17, 2010
    caciocode, Jul 17, 2010 IP
  4. phpl0v3r

    phpl0v3r Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    it is damn simple..
    use php curl
    then use preg match after observing the source code of the webpage
    now you have the price in variable just store it :)
     
    phpl0v3r, Jul 17, 2010 IP
  5. caciocode

    caciocode Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well, If think I about it like phpl0v3r has just put it, it sounds simple that way.
     
    caciocode, Jul 17, 2010 IP
  6. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #6
    I agree with phpl0v3r's solution. This is merely identifying right place on page to match using preg_match and you will have the price value with you :)
     
    mastermunj, Jul 17, 2010 IP
  7. adamsinfo

    adamsinfo Greenhorn

    Messages:
    60
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #7
    You can use curl with PHP and try and scrape the page as others have suggested, or you can use the DOM, i.e. get 'simplehtmldom' class. It will make things MUCH easier.
     
    adamsinfo, Jul 17, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    <?php
    //item to search...
    $item = '123456';
    
    //searches @ amazon...
    $amazon = file_get_contents('http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords='.urlencode($item).'&x=15&y=14&ih=1_0_0_0_0_0_0_0_0_1.418_18&fsc=-1');
    
    //find the price...
    if (preg_match('~<a href="http://www\.amazon\.com/.+?">Buy new</a>:&nbsp;<span>([\$0-9\.]+)</span></div>\s*<div class="usedPrice">&nbsp;</div>~', $amazon, $a)) {
    
    $price = $a[1];
    
    echo $price;
    
    //use fwrite() to write $price to txt file...
    }
    ?>
    PHP:
     
    danx10, Jul 17, 2010 IP
  9. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #9
    I do not recommend raw html parsing, most web 2.0 nowadays uses api, so use that.
     
    Kaizoku, Jul 17, 2010 IP
  10. phpl0v3r

    phpl0v3r Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    this comment helped me alot
    will look into it never knew such thing existed :)
    i hate regex :p
     
    phpl0v3r, Jul 17, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    True, but my post was a raw example - proof of concept.

    the DOM is based on regular expressions.
     
    danx10, Jul 17, 2010 IP
  12. phpl0v3r

    phpl0v3r Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    atleast, that saves me using regex :)
     
    phpl0v3r, Jul 17, 2010 IP
  13. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #13
    Just so you know, using their api does not require regex, they provide all the abstraction layer. So why waste your own cpu power when you can use theirs.
     
    Kaizoku, Jul 17, 2010 IP
  14. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #14
    True but its the equivalent in doing so, but in a more convenient way. (It saves your writing the regex, but not using it.)

    Also, I'd post up an example on how to use the amazon api up unfourtanetly im unfamiliar with it never used amazon nor their api, but as its an api i assume it would'nt be too hard to use - as it would have documentation an examples.
     
    danx10, Jul 17, 2010 IP
  15. z0e0u0s

    z0e0u0s Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Yeah if your going to be working with the Amazon I totally recomend using the API so much easier to use than parsing the HTML

    https: //affiliate-program.amazon.com/gp/advertising/api/detail/main.html#resources <----- I just signed up so I can't post links remove the space after the colon :D

    I use this API whenever I have to work with Amazon and there are some samples in there to help you get started, but it all depends at what level you are. If you have any questions hit me up!
     
    z0e0u0s, Jul 20, 2010 IP