Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用API&;获取Tumblr文章标题;PHP?_Php_Api_Simplexml_Tumblr - Fatal编程技术网

使用API&;获取Tumblr文章标题;PHP?

使用API&;获取Tumblr文章标题;PHP?,php,api,simplexml,tumblr,Php,Api,Simplexml,Tumblr,使用post和,我试图嵌入我网站上任何最新博客条目的标题 出于某种原因,我认为这将是一个很好的简单代码,但它返回空。我错过了什么明显的东西吗 // Contents of includes/latest_blog.php <?php $request_url = 'http://###NAME###.tumblr.com/api/read?start=0&num=1'; $xml = simplexml_load_file($request_url); $title =

使用post和,我试图嵌入我网站上任何最新博客条目的标题

出于某种原因,我认为这将是一个很好的简单代码,但它返回空。我错过了什么明显的东西吗

// Contents of includes/latest_blog.php
<?php
  $request_url = 'http://###NAME###.tumblr.com/api/read?start=0&num=1';
  $xml = simplexml_load_file($request_url);
  $title = $xml->posts->post->{‘regular-title’};
  $link = $xml->posts->post[‘url’];
  echo 'Latest blog entry: <a href="'.$link.'">'.$title.'</a>';
?>
//includes/latest_blog.php的内容
posts->post->{'regular-title'};
$link=$xml->posts->post['url'];
回显“最新博客条目:”;
?>
网站:

<p class="blog_title"><?php include('includes/latest_blog.php'); ?></p>


谢谢

你知道哪里是正确的道路。下面是代码(它可以工作,但我无法找到没有foreach的工作解决方案),但无论如何,您应该记住title是一个可选字段,它可以是空字符串,因此在构建链接标记之前需要检查它

$request_url = 'http://YOURNAME.tumblr.com/api/read?start=0&num=1';
$xml = simplexml_load_file($request_url);
$posts = $xml->xpath("/tumblr/posts/post"); 
foreach($posts as $post) {
  $url = $post['url-with-slug'];
  $tittle = $post->{'regular-title'};
  echo '<a href="'.$url.'">'.$tittle.'</a>';
}
$request\u url='1http://YOURNAME.tumblr.com/api/read?start=0&num=1';
$xml=simplexml\u load\u文件($request\u url);
$posts=$xml->xpath(“/tumblr/posts/post”);
foreach($posts作为$post){
$url=$post['url-with-slug'];
$tittle=$post->{'regular-title'};
回声';
}

事实上,代码很好,只是在
'regular-title'
中使用了奇特的Unicode引号,而不是
'regular-title'中的ASCII单引号

不正确:

$title = $xml->posts->post->{‘regular-title’};
$link = $xml->posts->post[‘url’];
正确:

$title = $xml->posts->post->{'regular-title'};
$link = $xml->posts->post['url'];
由于某些原因,您一定错过了错误消息,因此请确保在测试新代码时看到所有错误:

ini_set('display_errors', true);
error_reporting(-1);