Tumblr XML没有加载

时间:2012-11-16 05:18:16

标签: php simplexml tumblr

我根据我在另一个问题中找到的一些代码实现了以下代码:
Select specific Tumblr XML values with PHP

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://ACCOUNT.tumblr.com/api/read?type=photo&start=0&num=30";
//$request_url = "tumblr.xml";
$xml = simplexml_load_file($request_url);

foreach ($xml->posts->post as $post) {
echo "<div class=\"item\"><a href='".$post['url']."'><img src='".getPhoto($post->{'photo-url'}, 250)."' width=\"250\" /></a></div>"; 
}

这个代码在我的开发网站上运行得很好,但是当我在另一台服务器上实时推送它时,它不会从Tumblr加载外部XML ...它很好地加载了本地文本XML(在代码中注释掉)

我正在等待从客户端获取帐户凭据,因此我可以联系客户服务并与他们合作......

与此同时,有没有人有任何想法可能会导致这种情况? 服务器设置?
缺少PHP代码?

1 个答案:

答案 0 :(得分:0)

所以,我最终使用cURL来加载XML ......这是有效的代码:

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;
}

$url="http://ACCOUNT.tumblr.com/api/read?type=photo&start=0&num=30";
$ch = curl_init();      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents

$data = curl_exec($ch); // execute curl request
curl_close($ch); // close curl request

$xml = new SimpleXMLElement($data);

foreach($xml->posts->post as $post) {
    echo "<div class=\"item\"><a href='".$post['url']."'><img src='".getPhoto($post->{'photo-url'}, 250)."' width=\"250\" /></a></div>"; 
}