How to copy data from another site with PHP?

-1

I need to get data from another site. For example, I have a form in my control panel as follows:

<input type='text' name='navio'><br />
<input type='submit' value='consultar'/>

I need a script that when I type the name of the navio in the text box, it does the search in that site.

How do I get it to on this site the line that navio is located and play in my database?

    
asked by anonymous 05.11.2014 / 19:42

2 answers

3

There are two possibilities:

  • Read the site and parse it with Regular Expressions
  • Syntactically parse HTML with DOM or SimpleXML
  • The first option is the easiest but not the safest one for you, because if you do not beware of regular expression construction, a comma (literally) that the target site developer modifies and your app can potentially fail work.

    In addition, it is slower because you almost work on brute force, marrying different patterns, and manipulating array structures, often multidimensional.

    For this possibility file_get_contents () is often enough:

    $html = file_get_contents( 'http://www.site.com' );
    

    And $ html you enter as a succession target preg_match () , preg_match_all () , preg_replace () ... those which you think is best, as many times as you need.

    The second possibility is more complicated if you choose DOM , but it is safer because you work with the HTML hierarchy, almost the same in JavaScript. You list us, iterates collections of children, and so on.

    It's tricky because DOM is a massive and very detailed class set.

    If the target site is simpler, you can choose to SimpleXML which is like DOM , but much less powerful and consequently much simpler.

        
    05.11.2014 / 20:02
    0

    You will need to use some WebService.

    Make a request at a given address by passing the ship name as parameter and this address returns you a JSON containing the data for that parameter in JSON format.

    Here is a slide explaining how to create a webservice

    link

    You can not do what you want if the other site is a third party and the address you want to request is not a webservice address.

        
    05.11.2014 / 19:52