Get content from flickr

0

I wanted to get help with getting the contents of a page by the class, ie, to get all the html inside a div whose class is equal to the one informed.

Below is what I have been able 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);
    
asked by anonymous 19.02.2018 / 03:38

1 answer

0

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

You must close the connection after curl_exec . And if you do not have SSL on your site, you must add the CURLOPT_SSL_VERIFYPEER option to 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

    
19.02.2018 / 06:10