2012-11-16 52 views
4

我實現下面的代碼基於一些代碼,我在另一個問題發現:
Select specific Tumblr XML values with PHP的tumblr 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; 
} 

$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代碼?

+0

它出現在託管環境具有以下設置: allow_url_fopen選項:OFF allow_url_include:OFF cURL已啓用...我有點破解,但通過谷歌我得出的結論是,這是實現同樣的事情,但更安全的替代方法... 任何意見如何翻譯代碼來捲曲?謝謝。 – user1828635

回答

0

所以,我結束了使用捲曲加載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>"; 
}