我想用代码
进行API调用<?php
$url = "url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));
$data = curl_exec($ch);
echo $data;
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
?>
并希望响应会像这样
<GoodreadsResponse>
<GoodreadsResponse>
<Request>
<!-- ... request metadata omitted ... -->
</Request>
<search>
<query>
<![CDATA[ Ender's Game ]]>
</query>
<results-start>1</results-start>
<results-end>10</results-end>
<total-results>100</total-results>
<source>Goodreads</source>
<query-time-seconds>0.10</query-time-seconds>
</search>
....
</GoodreadsResponse>
但是当我执行该代码时,它会在浏览器中返回
true 1 20 386 Goodreads 0.39 2422333 207 690938 33000 1985 4.28 375802 589 Orson Scott Card https://d.gr-assets.com/book ......
如何解决此问题
答案 0 :(得分:1)
在浏览器中使用时,显示源代码(右键单击 - &gt;显示源代码)以查看未渲染的视图。
<?php
$request = "";
$curl = curl_init("https://www.goodreads.com/search.xml?key=9ZzEFHzs9LwIdA3qt0fMw&q=Ender%27s+Game");
curl_setopt($curl, CURLOPT_VERBOSE, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($curl, CURLOPT_TIMEOUT, 0);
$response = curl_exec($curl);
echo "<pre>"; print_r($response); echo "</pre>";
curl_close($curl);
答案 1 :(得分:0)
当我在浏览器中粘贴时,此URL返回正确的XML。
https://www.goodreads.com/search.xml?key=9ZzEFHzs9LwIdA3qt0fMw&q=Ender%27s+Game
您的浏览器可能正在将XML显示为文本。查看页面源,查看是否可以查看完整的XML响应。
此外,您只需在此处粘贴API密钥即可供所有人查看。您可能希望立即更改它以避免其他人使用/滥用它时出现问题。