How to copy data from another site with PHP?

Asked

Viewed 2,961 times

-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 in the name of navio in the text box, it does the search on that site.

How to make him take on that website the line that the navio locate and play in my database?

  • The entire site? a specific page or pages of the site? Just that specific site?

2 answers

3

There are two possibilities:

  1. Read the site and analyze it with Regular Expressions
  2. Syntactically parse HTML with GIFT or Simplexml

The first option is the easiest but not the safest for you because if you do not take precautions in the construction of Regular Expressions, a comma (literally) that the developer of the target site modifies and your Application may potentially fail to work.

In addition, it is slower because you almost work in brute force, marrying various patterns and manipulating arrays structures, often multidimensional.

For that possibility file_get_contents() often enough:

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

And $html you report as target of successive preg_match(), preg_match_all(), preg_replace()... those you find best, as many times as you need.

The second possibility is more complicated if you choose GIFT, but it’s safer because you work with the hierarchy of HTML, almost the same in Javascript. You list us, iterate collections of children and etc.

It’s complicated because the GIFT is a massive and very detailed set of classes.

If the target site is simpler, you can choose Simplexml which is kind to GIFT, but much less powerful and therefore much simpler.

0

You will need to use some Webservice.

Make a request on a given address passing the name of the ship as parameter and this address returns a JSON containing the data referring to this parameter in JSON format.

Here is a slide explaining how to create a webservice

http://pt.slideshare.net/MarcioJuniorVieira/criando-e-consumindo-webservice-rest-com-php-e-json

There is no way to do what you want, if the other site is third party and the address you want to order is not a web service address.

  • I’m sorry, man, but you’re a good ride on this one. The creation of a Webservice is the responsibility of the website developer IF he wants to offer a clean and concise way for other applications to consume specific data without the full burden of syntactic or regular analysis.

  • Until, then he took what I said and wrote beautiful....

  • 1

    No, I just answered right. And politely.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.