Php 我将Goodreads API结果从XML转换为JSON的方法出了什么问题?
我有一个名为bookscript.PHP的PHP页面,专门用于将Goodreads API结果从XML格式转换为JSON格式:Php 我将Goodreads API结果从XML转换为JSON的方法出了什么问题?,php,json,xml,api,Php,Json,Xml,Api,我有一个名为bookscript.PHP的PHP页面,专门用于将Goodreads API结果从XML格式转换为JSON格式: <?php $xml_string = file_get_contents("https://www.goodreads.com/search?q=" .$_REQUEST['search']. "&search%5Bfield%5D=title&format=xml&key=ZuqW9sL15d3JvEwmLyaNCg");
<?php
$xml_string = file_get_contents("https://www.goodreads.com/search?q=" .$_REQUEST['search']. "&search%5Bfield%5D=title&format=xml&key=ZuqW9sL15d3JvEwmLyaNCg");
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
echo $json;
?>
但这不起作用。当我访问时,页面上显示的只是“false”一词
将此结果与Goodreads的实际结果进行比较:
所以我的问题是。。。我的方法出了什么问题?我确实在我的网站上使用SSL(不确定这是否相关),所以应该排除这个问题
我知道GET请求代码应该是有效的,因为该代码适用于其他使用JSON格式的API
编辑:问题实际上是我的服务器上没有安装PHP
json\u encode
需要一个带有公共属性的数组或对象作为输入。如果失败,则返回布尔值false
simplexml\u load\u字符串
返回一个simplexml元素
您需要在simplexmlement
上循环,并从中创建一个数组,或者查找为您执行此操作的函数或类。您可以在internet上找到许多实现
看到这个答案:我已经用这段代码进行了测试,一切都很好
<?php $xml_string = file_get_contents("https://www.goodreads.com/search?q=dog&search%5Bfield%5D=title&format=xml&key=ZuqW9sL15d3JvEwmLyaNCg");
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);?>
<pre>
<?php print_r($array); ?>
</pre>
也许你的客户有错误?ajax调用的结果是什么
simplexml\u load\u string
返回simplexmlement对象。从文档中我不认为它是json可序列化的:您可能必须从它创建一个数组。请尝试data.search.results | |[]而不是data.results这是最奇怪的事情。我现在发现它在我的本地主机上工作(而且非常好!),但在我真正的服务器上不工作(本地文件和服务器文件之间的代码完全相同)。你能猜出是什么原因造成的吗?谢谢。首先,尝试手动检查php脚本是否有效。就像我的“例子”。然后,您可以看到它是在服务器端,还是另一个错误。如果php脚本不起作用,那么请检查php的版本,可能simplexml已关闭,朋友,非常感谢。你的解决方案已经解决了我的问题,现在效果非常好。再次感谢。不客气)如果可以,请写下您的问题以及如何解决。那是什么?也许对未来的观众会有帮助。
<?php $xml_string = file_get_contents("https://www.goodreads.com/search?q=dog&search%5Bfield%5D=title&format=xml&key=ZuqW9sL15d3JvEwmLyaNCg");
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);?>
<pre>
<?php print_r($array); ?>
</pre>
Array
(
[Request] => Array
(
[authentication] => true
[key] => Array
(
)
[method] => Array
(
)
)
[search] => Array
(
[query] => Array
(
)
[results-start] => 1
[results-end] => 20
[total-results] => 26080
[source] => Goodreads
[query-time-seconds] => 0.31
[results] => Array
(
[work] => Array
(
[0] => Array
(
[id] => 10479953
[books_count] => 38...