使用 PHP 选择特定的 Tumblr XML 值 [英] Select specific Tumblr XML values with PHP
问题描述
我的目标是使用他们提供的 XML 将 Tumblr 帖子嵌入到网站中.问题是 Tumblr 会为您发布的每张图片保存 6 种不同尺寸的图片.我下面的代码将获得第一张图像,但它恰好太大了.如果所有照片都具有相同的
标签,如何从 XML 中选择一张较小尺寸的照片?
→这是我正在使用的 Tumblr 中的 XML:Tumblr XML.
→到目前为止,这是我的 PHP 代码:
posts->post->{'photo-caption'};$photo = $xml->posts->post->{'photo-url'};echo '<h1>'.$title.'</h1>';echo '<img src="'.$photo.'"/>"';回声……";echo "</br><a target=frame2 href='".$link."'>阅读更多</a>";?>
getPhoto
函数接受一个 $photos
数组和一个 $desiredWidth
>.它返回 max-width
(1) 最接近和 (2) 小于或等于 $desiredWidth
的照片.您可以调整该功能以满足您的需要.需要注意的重要事项是:
$xml->posts->post->{'photo-url'}
是一个数组.$photo['max-width']
访问
标签上的max-width
属性.
我使用了 echo '
';print_r($xml->posts->post);echo '</pre>'; 找出$xml->posts->post->{'photo-url'}
是一个数组.我在
SimpleXMLElement
.function getPhoto($photos, $desiredWidth) {$currentPhoto = NULL;$currentDelta = PHP_INT_MAX;foreach ($photos as $photo) {$delta = abs($desiredWidth - $photo['max-width']);如果 ($photo['max-width'] <= $desiredWidth && $delta < $currentDelta) {$currentPhoto = $photo;$currentDelta = $delta;}}返回 $currentPhoto;}$request_url = "http://kthornbloom.tumblr.com/api/read?type=photo";$xml = simplexml_load_file($request_url);foreach ($xml->posts->post as $post) {echo '<h1>'.$post->{'photo-caption'}.'</h1>';echo '<img src="'.getPhoto($post->{'photo-url'}, 450).'"/>"';echo "...";echo "</br><a target=frame2 href='".$post['url']."'>阅读更多</a>";}
My goal is to embed Tumblr posts into a website using their provided XML. The problem is that Tumblr saves 6 different sizes of each image you post. My code below will get the first image, but it happens to be too large. How can I select one of the smaller-sized photos out of the XML if all the photos have the same tag of
<photo-url>
?→ This is the XML from my Tumblr that I'm using: Tumblr XML.
→ This is my PHP code so far:
<?php $request_url = "http://kthornbloom.tumblr.com/api/read?type=photo"; $xml = simplexml_load_file($request_url); $title = $xml->posts->post->{'photo-caption'}; $photo = $xml->posts->post->{'photo-url'}; echo '<h1>'.$title.'</h1>'; echo '<img src="'.$photo.'"/>"'; echo "…"; echo "</br><a target=frame2 href='".$link."'>Read More</a>"; ?>
解决方案The function
getPhoto
takes an array of$photos
and a$desiredWidth
. It returns the photo whosemax-width
is (1) closest to and (2) less than or equal to$desiredWidth
. You can adapt the function to fit your needs. The important things to note are:
$xml->posts->post->{'photo-url'}
is an array.$photo['max-width']
accesses themax-width
attribute on the<photo>
tag.
I used echo '<pre>'; print_r($xml->posts->post); echo '</pre>';
to find out $xml->posts->post->{'photo-url'}
was an array.
I found the syntax for accessing attributes (e.g., $photo['max-width']
) at the documentation for SimpleXMLElement
.
function getPhoto($photos, $desiredWidth) {
$currentPhoto = NULL;
$currentDelta = PHP_INT_MAX;
foreach ($photos as $photo) {
$delta = abs($desiredWidth - $photo['max-width']);
if ($photo['max-width'] <= $desiredWidth && $delta < $currentDelta) {
$currentPhoto = $photo;
$currentDelta = $delta;
}
}
return $currentPhoto;
}
$request_url = "http://kthornbloom.tumblr.com/api/read?type=photo";
$xml = simplexml_load_file($request_url);
foreach ($xml->posts->post as $post) {
echo '<h1>'.$post->{'photo-caption'}.'</h1>';
echo '<img src="'.getPhoto($post->{'photo-url'}, 450).'"/>"';
echo "...";
echo "</br><a target=frame2 href='".$post['url']."'>Read More</a>";
}
这篇关于使用 PHP 选择特定的 Tumblr XML 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!