1

Yahooのコンテンツ分析を使おうとしていますが、ここからはとても使いやすいようです

しかし、コードを実行するたびに、次の出力がそのまま表示されます。

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

私が欲しいのは、このリンクをクリックしたときのように、XMLタグで構造化されたXMLドキュメントを表示することです。

また、出力として表示されているもののソースコード(ブラウザから..右クリック>ソースを表示)は次のとおりです。

<?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

以下は私のコードです:

<?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;
?>
4

3 に答える 3

4

これは、送信されるヘッダーがContent-type: text/html. フォーマットされた XML を示すリンク先のデモの例では、特別なフォーマットを使用して、そのように見せています。ヘッダーを text/xml のように設定する必要がありheader('Content-type: text/xml');、出力はフォーマットされて表示されます。

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

次のようにコンテンツを出力することもできます。

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

上記は、ブラウザーで XML がフォーマットされていない理由を説明し、それを修正する方法を示しています。OP の主な問題は、出力の最後にある迷子の文字列が原因で、XML の形式が正しくないことです。以下はそれを扱います:

$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;
于 2012-11-24T06:06:45.060 に答える
1

その結果がブラウザに表示された場合。ソースを表示するだけです。これにより、タグを含むすべてが表示されます。brousr はタグを表示せず、コンテンツのみを表示します。

于 2012-11-24T06:05:35.413 に答える
0

メソッドを使用してHTTP ヘッダーheaderを指定していません。Content-Typeその結果、PHP はデフォルトの Content-Type を出力しtext/html、ブラウザーは XML マークアップを無効な HTML として処理しています。

データの正しいコンテンツ タイプを出力します。

header("Content-Type: application/xml");
于 2012-11-24T06:08:19.750 に答える