Last active
August 29, 2015 13:57
-
-
Save NiciusB/9843822 to your computer and use it in GitHub Desktop.
OBTENCIÓN DE POSTS DE TUMBLR SEGÚN BÚSQUEDA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$busqueda="php"; // Texto a buscar | |
$n=1; // Número de página | |
$request = 'http://www.tumblr.com/search/'.$busqueda; | |
$fields = array( | |
'post_page' => urlencode($n), | |
'post_view' => urlencode('list') | |
); | |
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } | |
rtrim($fields_string, '&'); | |
$ci = curl_init($request); | |
curl_setopt($ci,CURLOPT_POST, count($fields)); | |
curl_setopt($ci,CURLOPT_POSTFIELDS, $fields_string); | |
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE); | |
$input = curl_exec($ci); | |
$dom = new DomDocument(); | |
@$dom->loadHTML($input); | |
$finder = new DomXPath($dom); | |
$nodes = $finder->evaluate( | |
"/html/body//div[@class='post_wrapper']" | |
); | |
foreach($nodes as $div) { | |
$newdom = new DOMDocument; | |
@$newdom->loadHTML(get_inner_html($div)); | |
$finder = new DomXPath($newdom); | |
$image = $newdom->getElementsByTagName('img')->item(0); | |
$link = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), 'post_permalink')]")->item(0); | |
if(is_object($image)) { | |
$imagen = $image->getAttribute('src'); // URL de la imagen | |
$url = $link->getAttribute('href'); // URL del post en tumblr | |
echo "<a href='$url'><img src='$imagen' alt='Imagen'/></a><br>"; | |
} | |
} | |
function get_inner_html( $node ) { | |
$innerHTML= ''; | |
$children = $node->childNodes; | |
foreach ($children as $child) { | |
$innerHTML .= $child->ownerDocument->saveXML( $child ); | |
} | |
return $innerHTML; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment