Using data from another site via LINK / ID

0

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

Ex: My site has the ('Title, Image, Tracks.') fields and there is a site with a database with the same content type as mine, so the balcony is , Ex: I will publish information of a disc of an artist, I need instead of having to put everything manually given by die, I only put the link of the site with the search referring to the artist's disc and through that 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 that I put.

I just need to know if I can do this, and if possible how to do it.

    
asked by anonymous 04.09.2018 / 21:55

2 answers

2

You do not need to download an entire lib as link 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 sites "require" the user-agent, so you can do this:

$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 lock ALLOW_URL_FOPEN then you can use curl.

    
04.09.2018 / 23:45
1

It is possible, 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>';
}
    
04.09.2018 / 23:23