5

我根据在另一个问题中找到的一些代码实现了以下代码:
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 代码?

4

1 回答 1

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>"; 
}
于 2012-11-17T00:47:49.733 回答