How to use data from another site via LINK/ID

Asked

Viewed 45 times

0

I need to know how it is possible for me to fill out data on my site with data from another site.

Ex: My site has the fields ('Title, Image, Tracks.') And there’s a site with a database with the same kind of content as mine, so the point is, I’m going to publish information from an artist’s disk, I need that instead of having to put everything manually given out, I put only the link of the site with the search referring to the artist’s disc and through this link my site would recognize the fields with the data automatically and link in my publication the data of the other site referring to the link I placed.

I just need to know if you have how to do it, and if possible how to do it.

2 answers

2


No need to download a whole lib as http://simplehtmldom.sourceforge.net/ for something so simple.

Just use what you already have native:

$dom = new DOMDocument;
$dom->load('http://www.google.com/');
$ancoras = $dom->getElementsByTagName('img');

foreach($ancoras as $elementos) {
   echo $elementos->getAttribute('src'), '<hr>';
}

Note that some websites "require" the user-agent, so you can do so:

$url = 'http://www.google.com/';

$headers = array(
    'Accept-language: pt-br',
    'User-Agent: ' . $_SERVER['HTTP_USER_AGENT']
);

$opts = array(
    'http'=>array(
        'method' => 'GET',
        'header' => implode(PHP_EOL, $headers)
    )
);

$context = stream_context_create($opts);
$result = file_get_contents($url, NULL, $context);

$dom = new DOMDocument;
$dom->loadHTML($result);
$ancoras = $dom->getElementsByTagName('img');

foreach($ancoras as $elementos) {
   echo $elementos->getAttribute('src'), '<hr>';
}

If you have blockage of ALLOW_URL_FOPEN then you can use Curl.

1

It is possible yes, using PHP Simple HTML DOM Parser, a very simple example would be this:

// Recuperando HTML da página com base na URL
$html = file_get_html('http://www.google.com/');

// Buscando todas as imagens
foreach($html->find('img') as $element) {
    echo $element->src . '<br>';
}

Browser other questions tagged

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