将使用cURL生成的XML文件转换为JSON

时间:2017-11-22 15:49:18

标签: php json xml curl

我正在使用boardgamegeek.com XML API获取有关游戏的信息。我能够使用cURL来检索它,但现在我有了XML数据,我似乎无法用它做任何事情。

这是我的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);

这是我生成的输出:

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>

我尝试将代码更改为以下内容以尝试生成JSON数组:

<?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星际迷航:Ascendancy - Borg同化本页   不存在。您可以编辑本页面来创建它。   https://cf.geekdo-images.com/images/pic3690765_t.jpg   https://cf.geekdo-images.com/images/pic3690765.jpg 4X游戏动作   点数津贴制度文明骰子滚动Aaron Dill Katie   Dillon英文第一版Exploration Gale Force Nine,LLC约翰   Kovaleski模块化电路板电影/电视/广播主题谈判科学   小说太空探索星际迷航Sean Sweigart领土大厦   交易可变相序变量播放器为战争带来了什么   你本周播放播客第145周查尔斯伍兹星际迷航:   支配警告:simplexml_load_string():实体:第1行:解析器   错误:期望开始标记,'&lt;'找不到   第19行/home/matthew1/public_html/api/index.php

     

警告:simplexml_load_string():1 in   第19行/home/matthew1/public_html/api/index.php

     

警告:simplexml_load_string():^ in   /home/matthew1/public_html/api/index.php第19行布尔(假)

(注意在错误消息开始之前显示的XML数据没有<tags>。)

1 个答案:

答案 0 :(得分:1)

您忘记设置选项CURLOPT_RETURNTRANSFER

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  

TRUE以将返回值作为字符串返回   curl_exec()而不是直接输出(source)。

在此分配$response = curl_exec($ch);后没有此选项,$response变量将包含布尔值true。然后在执行代码$xml = simplexml_load_string($response);后出现错误:

  

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

要发送有效的XML标头,请使用:

curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: text/xml"]); 
相关问题