1

I want to make an API call with code

<?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);
?>

and hope that the response will be like this

<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>

but when I execute that code, it returns in browser like this

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 ......

How can I fix this

2 Answers 2

1

when use in Browser, than show Sourcecode (rightclick -> show sourcecode) for unrendered view.

<?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);
Sign up to request clarification or add additional context in comments.

Comments

0

This URL when I paste in the browser returns correct XML.

https://www.goodreads.com/search.xml?key=9ZzEFHzs9LwIdA3qt0fMw&q=Ender%27s+Game

Your browser may be displaying the XML as text. View the source of the page and see if you can view the full XML response.

Also you just pasted the API key here for everyone to see. You might want to change it now to avoid problems with anyone else using/abusing it.

1 Comment

Oh yes, browser handle response as html, not xml, how can I fix this, I have set content-type : text/xml but it doesnot work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.