2017-11-22 9 views
0

boardgamegeek.com XML APIを使用してゲームに関する情報を取得しています。私はこれを取得するためにcURLを使用することができますが、今はXMLデータを持っているので、何もできないようです。cURLで生成されたXMLファイルをJSONに変換する

これは私のcURLのコードです:

<?php 
header('Content-type: text/xml'); 

$id = 226910; 
$exp = 1; 

$url = 'https://www.boardgamegeek.com/xmlapi/' . (!$exp ? 'boardgame' : 'boardgameexpansion') . '/' . $id . '/'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 

$xml = curl_exec($ch); 
if ($xml == false) 
{ 
    echo curl_error($ch); 
} 
curl_close($ch); 

そして、これは私の生成出力されます:私はJSON配列をしようとして生成するには、次の私のコードを変更

This XML file does not appear to have any style information associated with it. The document tree is shown below. 
<boardgames termsofuse="http://boardgamegeek.com/xmlapi/termsofuse"> 
<boardgame objectid="226910"> 
<yearpublished>2017</yearpublished> 
<minplayers>1</minplayers> 
<maxplayers>5</maxplayers> 
<playingtime>0</playingtime> 
<minplaytime>0</minplaytime> 
<maxplaytime>0</maxplaytime> 
<age>14</age> 
<name primary="true" sortindex="1">Star Trek: Ascendancy – Borg Assimilation</name> 
<description> 
This page does not exist. You can edit this page to create it. 
</description> 
<thumbnail> 
https://cf.geekdo-images.com/images/pic3690765_t.jpg 
</thumbnail> 
<image>https://cf.geekdo-images.com/images/pic3690765.jpg</image> 
<boardgamefamily objectid="12210">4X games</boardgamefamily> 
<boardgamemechanic objectid="2001">Action Point Allowance System</boardgamemechanic> 
<boardgamecategory objectid="1015">Civilization</boardgamecategory> 
<boardgamemechanic objectid="2072">Dice Rolling</boardgamemechanic> 
<boardgamedesigner objectid="62674">Aaron Dill</boardgamedesigner> 
<boardgameartist objectid="76681">Katie Dillon</boardgameartist> 
<boardgameversion objectid="354684">English first edition</boardgameversion> 
<boardgamecategory objectid="1020">Exploration</boardgamecategory> 
<boardgamepublisher objectid="11420">Gale Force Nine, LLC</boardgamepublisher> 
<boardgamedesigner objectid="62672">John Kovaleski</boardgamedesigner> 
<boardgamemechanic objectid="2011">Modular Board</boardgamemechanic> 
<boardgamecategory objectid="1064">Movies/TV/Radio theme</boardgamecategory> 
<boardgamecategory objectid="1026">Negotiation</boardgamecategory> 
<boardgamecategory objectid="1016">Science Fiction</boardgamecategory> 
<boardgamecategory objectid="1113">Space Exploration</boardgamecategory> 
<boardgamefamily objectid="7392">Star Trek</boardgamefamily> 
<boardgamedesigner objectid="62673">Sean Sweigart</boardgamedesigner> 
<boardgamecategory objectid="1086">Territory Building</boardgamecategory> 
<boardgamemechanic objectid="2008">Trading</boardgamemechanic> 
<boardgamemechanic objectid="2079">Variable Phase Order</boardgamemechanic> 
<boardgamemechanic objectid="2015">Variable Player Powers</boardgamemechanic> 
<boardgamecategory objectid="1019">Wargame</boardgamecategory> 
<boardgamepodcastepisode objectid="213535">What Did You Play This Week Podcast Week 145</boardgamepodcastepisode> 
<boardgameartist objectid="62716">Charles Woods</boardgameartist> 
<boardgameexpansion objectid="193949" inbound="true">Star Trek: Ascendancy</boardgameexpansion> 
<poll name="suggested_numplayers" title="User Suggested Number of Players" totalvotes="1"> 
<results numplayers="1"> 
<result value="Best" numvotes="1"/> 
<result value="Recommended" numvotes="0"/> 
<result value="Not Recommended" numvotes="0"/> 
</results> 
<results numplayers="2"> 
<result value="Best" numvotes="1"/> 
<result value="Recommended" numvotes="0"/> 
<result value="Not Recommended" numvotes="0"/> 
</results> 
<results numplayers="3"> 
<result value="Best" numvotes="1"/> 
<result value="Recommended" numvotes="0"/> 
<result value="Not Recommended" numvotes="0"/> 
</results> 
<results numplayers="4"> 
<result value="Best" numvotes="1"/> 
<result value="Recommended" numvotes="0"/> 
<result value="Not Recommended" numvotes="0"/> 
</results> 
<results numplayers="5"> 
<result value="Best" numvotes="0"/> 
<result value="Recommended" numvotes="0"/> 
<result value="Not Recommended" numvotes="0"/> 
</results> 
<results numplayers="5+"> 
<result value="Best" numvotes="0"/> 
<result value="Recommended" numvotes="0"/> 
<result value="Not Recommended" numvotes="0"/> 
</results> 
</poll> 
<poll name="language_dependence" title="Language Dependence" totalvotes="0"> 
<results> 
<result level="1" value="No necessary in-game text" numvotes="0"/> 
<result level="2" value="Some necessary text - easily memorized or small crib sheet" numvotes="0"/> 
<result level="3" value="Moderate in-game text - needs crib sheet or paste ups" numvotes="0"/> 
<result level="4" value="Extensive use of text - massive conversion needed to be playable" numvotes="0"/> 
<result level="5" value="Unplayable in another language" numvotes="0"/> 
</results> 
</poll> 
<poll name="suggested_playerage" title="User Suggested Player Age" totalvotes="1"> 
<results> 
<result value="2" numvotes="0"/> 
<result value="3" numvotes="0"/> 
<result value="4" numvotes="0"/> 
<result value="5" numvotes="0"/> 
<result value="6" numvotes="0"/> 
<result value="8" numvotes="0"/> 
<result value="10" numvotes="0"/> 
<result value="12" numvotes="0"/> 
<result value="14" numvotes="1"/> 
<result value="16" numvotes="0"/> 
<result value="18" numvotes="0"/> 
<result value="21 and up" numvotes="0"/> 
</results> 
</poll> 
</boardgame> 
</boardgames> 

を試してみました:

<?php 
// header('Content-type: text/xml'); 

$id = 226910; 
$exp = 1; 

$url = 'https://www.boardgamegeek.com/xmlapi/' . (!$exp ? 'boardgame' : 'boardgameexpansion') . '/' . $id . '/'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 

$response = curl_exec($ch); 
if ($response == false) 
{ 
    echo curl_error($ch); 
} 
curl_close($ch); 

$xml = simplexml_load_string($response); 
$json = json_encode($xml); 
$array = json_decode($json, true); 

var_dump($array); 

しかし、これはちょうど次のエラーをスローします:

2017 1 5 0 0 0 14 Star Trek: Ascendancy – Borg Assimilation This page does not exist. You can edit this page to create it. https://cf.geekdo-images.com/images/pic3690765_t.jpg https://cf.geekdo-images.com/images/pic3690765.jpg 4X games Action Point Allowance System Civilization Dice Rolling Aaron Dill Katie Dillon English first edition Exploration Gale Force Nine, LLC John Kovaleski Modular Board Movies/TV/Radio theme Negotiation Science Fiction Space Exploration Star Trek Sean Sweigart Territory Building Trading Variable Phase Order Variable Player Powers Wargame What Did You Play This Week Podcast Week 145 Charles Woods Star Trek: Ascendancy Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/matthew1/public_html/api/index.php on line 19

Warning: simplexml_load_string(): 1 in /home/matthew1/public_html/api/index.php on line 19

Warning: simplexml_load_string():^in /home/matthew1/public_html/api/index.php on line 19 bool(false)

(エラーメッセージが起動する前に<tags>せずに表示されているXMLデータを注意してください。)

+0

おかげで、[どのように匿名の... – mpdc

+0

可能な重複その後コード$xml = simplexml_load_string($response);を実行した後にエラーが発生しますxmlをphpの配列に変換しますか?](https://stackoverflow.com/questions/6578832/how-to-convert-xml-into-array-in-php) – hanshenrik

答えて

1

あなたはオプションCURLOPT_RETURNTRANSFER設定するのを忘れ:この割り当て後、このオプションを指定しない

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly (source).

$response = curl_exec($ch);の場合、$response変数にはブール値trueが含まれます。

Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found

有効なXMLヘッダーの使用を送信するには:よく説明downvoteのため

curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: text/xml"]); 
+0

@mpdc、これは問題の解決に役立ちましたか? – camelsWriteInCamelCase

関連する問題