dtjo51649 2012-11-24 05:59
浏览 54
已采纳

为什么我无法以XML格式查看结果?

I'm trying to use Yahoo's content analysis, which seems really easy to use from here

But whenever I execute my code, I get the following output, as it is:

Italian sculptors the Virgin Mary painters http://en.wikipedia.com/wiki/Painting http://en.wikipedia.com/wiki/Adobe_Photoshop http://en.wikipedia.com/wiki/Still_life http://en.wikipedia.com/wiki/Avant-garde http://en.wikipedia.com/wiki/In_the_Sky http://en.wikipedia.com/wiki/Potato 1

What I want is to see an XML document structured with the XML tags just like the way it appears when you click this link

Also, the source code (from the browser.. the right click>view source thing) of what I'm seeing as the output is:

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2012-11-24T05:54:55Z" yahoo:lang="en-US"><results><entities xmlns="urn:yahoo:cap">
    <entity score="0.784327">
      <text end="16" endchar="16" start="0" startchar="0">Italian sculptors</text>
    </entity>
    <entity score="0.78097">
      <text end="72" endchar="72" start="58" startchar="58">the Virgin Mary</text>
    </entity>
    <entity score="0.509566">
      <text end="29" endchar="29" start="22" startchar="22">painters</text>
      <wiki_url>http://en.wikipedia.com/wiki/Painting</wiki_url>
      <related_entities>
        <wikipedia>
          <wiki_url>http://en.wikipedia.com/wiki/Adobe_Photoshop</wiki_url>
          <wiki_url>http://en.wikipedia.com/wiki/Still_life</wiki_url>
          <wiki_url>http://en.wikipedia.com/wiki/Avant-garde</wiki_url>
          <wiki_url>http://en.wikipedia.com/wiki/In_the_Sky</wiki_url>
          <wiki_url>http://en.wikipedia.com/wiki/Potato</wiki_url>
        </wikipedia>
      </related_entities>
    </entity>
  </entities></results></query><!-- total: 191 -->
<!-- engine6.yql.ac4.yahoo.com -->
1

Following is my code :

<?php
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'http://query.yahooapis.com/v1/public/yql');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, "q=select * from contentanalysis.analyze where text='Italian sculptors and painters of the renaissance favored the Virgin Mary for inspiration';");
curl_setopt($c,CURLOPT_HEADER,0);
$op=curl_exec ($c);
curl_close ($c); 
echo $op;
?>
  • 写回答

3条回答 默认 最新

  • 我在西湖1 2012-11-24 06:06
    关注

    That is how XML is displayed in the browser when the header being sent is Content-type: text/html. The demo example you link to that shows formatted XML uses some special formatting to get it looking like that. You need to set the header to be text/xml like header('Content-type: text/xml'); and then the output should display formatted.

    header('Content-type: text/xml');
    echo $op;
    

    You can also output your content like so:

    echo '<pre>';
    echo htmlentities($op);
    echo '</pre>';
    

    The above explains why XML shows up unformatted in the browser and demonstrates how to fix that. The OP's main problem is that his XML is malformed due to that stray string at the the end of the output. The following deals with that:

    $r = 'http://query.yahooapis.com/v1/public/yql';
    $p = "q=select * from contentanalysis.analyze where text='Italian sculptors and painters of the renaissance favored the Virgin Mary for inspiration'"; 
    
    $c = curl_init($r);
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, $p);
    curl_setopt($c, CURLOPT_HEADER, true);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $op = curl_exec ($c);
    curl_close ($c); 
    
    if (!($xml = strstr($op, '<?xml'))) {
        $xml = null;
    }
    
    header('Content-type: text/xml');
    echo $xml;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了