Get content from flickr

Asked

Viewed 51 times

0

wanted a help to pick up the contents of a page by class, ie, take all the html inside a div whose class is equal to the informed.

Below is what I have managed to do so far.

$curl = curl_init();
$curl_setopt($curl, CURLOPT_URL, 'https://www.flickr.com/photos/esprit-confus/albums');
$curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
$html = curl_exec($curl);

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$albunsDiv = $xpath->query('//div[@class="view photo-list-view requiredToShowOnServer"]')->item(0);
echo $album = $dom->saveXML($albunsDiv);
  • At first I don’t know if their website allows this type of content capture, I advise reading the Flickr API documentation: https://www.flickr.com/services/api/

1 answer

0

Your code is not working because you are using curl_setopt as if it were a variable and not a native function of PHP.

You too must close the connection after curl_exec. And if you don’t have SSL on your site, you need to add the option CURLOPT_SSL_VERIFYPEER as false

<?php

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.flickr.com/photos/esprit-confus/albums');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$html = curl_exec($curl);
curl_close($curl);


$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$albunsDiv = $xpath->query('//div[@class="view photo-list-view requiredToShowOnServer"]')->item(0);

/* Utilizando informações com Regex */
foreach($albunsDiv->childNodes as $child) {
    preg_match("/href=\"(?P<href>.*?)\".*title=\"(?P<title>.*?)\"/", $dom->saveXML($child), $content);

    echo "<a href=\"{$content["href"]}\">{$content["title"]}</a><br>";
}

/* Exibindo todo o conteúdo */
//echo $album = $dom->saveXML($albunsDiv);

But depending on what you want, maybe it’s best to work with the API

Browser other questions tagged

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