我根据在另一个问题中找到的一些代码实现了以下代码:
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 代码?
最佳答案
所以,我最终使用 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>";
}
关于php - Tumblr XML 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410911/