2

Freebaseから結果のリストを取得しようとしています。MIDの配列があります。誰かがクエリを構造化してPHPのAPIに渡す方法を説明できますか?

私はMQLを初めて使用します-例を機能させることすらできないようです:

$simplequery = array('id'=>'/topic/en/philip_k_dick', '/film/writer/film'=>array());
$jsonquerystr = json_encode($simplequery);
// The Freebase API requires a query envelope (which allows you to run multiple queries simultaneously) so we need to wrap our original, simplequery structure in two more arrays before we can pass it to the API:
$queryarray = array('q1'=>array('query'=>$simplequery));
$jsonquerystr = json_encode($queryarray);
// To send the JSON formatted MQL query to the Freebase API use cURL:
#run the query
$apiendpoint = "http://api.freebase.com/api/service/mqlread?queries";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$apiendpoint=$jsonquerystr");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonresultstr = curl_exec($ch);
curl_close($ch);
// Decoding the JSON structure back into arrays is performed using json_decode as in:
$resultarray = json_decode($jsonresultstr, true); #true:give us the json struct as an array
// Iterating over the pieces of the resultarray containing films gives us the films Philip K. Dick wrote:
$filmarray = $resultarray["q1"]["result"]["/film/writer/film"];

foreach($filmarray as $film){
    print "$film<br>";
}
4

2 に答える 2

2

あなたはすべてを正しくやっています。そうでない場合は、JSON結果にエラーメッセージが返されます。

フィリップ・K・ディックのデータが更新され、彼を映画の「作家」ではなく「映画の原作者」として識別できるようになったと思います。(結局のところ、彼は実際に脚本を書いていませんでした。)

simplequeryを次の場所から変更します。

$simplequery = array('id'=>'/topic/en/philip_k_dick', '/film/writer/film'=>array());

に:

$simplequery = array('id'=>'/topic/en/philip_k_dick', '/film/film_story_contributor/film_story_credits'=>array());

実際には、Freebase Webサイトを使用してトピックにドリルダウンし、この情報を掘り下げることができますが、見つけるのはそれほど簡単ではありません。基本的なPhilipK.Dickページ(http://www.freebase.com/view/en/philip_k_dick)で、下部にある[編集して詳細を表示]ボタンをクリックします。

「編集」ページ(http://www.freebase.com/edit/topic/en/philip_k_dick)には、このトピックに関連するタイプが表示されます。このリストには「映画の原作者」は含まれていますが、「作家」は含まれていません。このページの映画の原作者ブロック内に、「詳細ビュー」リンク(http://www.freebase.com/view/en/philip_k_dick/-/film/film_story_contributor/film_story_credits)があります。これは、基本的に、PHPコードで複製しようとしているものです。

実際の脚本家(たとえば、スティーブマーティン)の同様のドリルダウンでは、/ film / writer / film(http://www.freebase.com/view/en/steve_martin/-/film/)というプロパティに移動します。脚本家/映画)。

複数のクエリ

MIDの配列で何をしようとしているのかを正確に言うことはできませんが、複数のクエリを実行するのは、すべて$ queryarray内にq2、q3などを追加するのと同じくらい簡単です。答えは同じ構造内に戻ります。q1データを引き出すのと同じように、答えを引き出すことができます。jsonquerystrとjsonresultstrを印刷すると、何が起こっているかがわかります。

于 2012-05-02T17:13:32.783 に答える
0

質問への回答を含めるように少し変更しました。これにより、それぞれに賛成票を投じることができました。次のように、より「完全な」回答を提供すると思いました。

$simplequery = array('id'=>'/topic/en/philip_k_dick', '/film/film_story_contributor/film_story_credits'=>array());
$jsonquerystr = json_encode($simplequery);
// The Freebase API requires a query envelope (which allows you to run multiple queries simultaneously) so we need to wrap our original, simplequery structure in two more arrays before we can pass it to the API:
$queryarray = array('q1'=>array('query'=>$simplequery));
$jsonquerystr = json_encode($queryarray);
// To send the JSON formatted MQL query to the Freebase API use cURL:
#run the query
$apiendpoint = "http://api.freebase.com/api/service/mqlread?queries";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$apiendpoint=$jsonquerystr");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonresultstr = curl_exec($ch);
curl_close($ch);
// Decoding the JSON structure back into arrays is performed using json_decode as in:
$resultarray = json_decode($jsonresultstr, true); #true:give us the json struct as an associative array
// Iterating over the pieces of the resultarray containing films gives us the films Philip K. Dick wrote:

if($resultarray['code'] == '/api/status/ok'){
    $films = $resultarray['q1']['result']['/film/film_story_contributor/film_story_credits'];
    foreach ($films as $film){
        print "$film</br>";
    }
}
于 2013-03-25T06:36:45.557 に答える