Reputation: 645
I'm trying to pull data using CURL in PHP like this
$ch = curl_init();
$headers = array('Content-type: text/xml',"openapikey:da21d9s56ekr33");
curl_setopt($ch, CURLOPT_URL, "http://api.test.co.un/rest/cateservice/category");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$return = curl_exec($ch);
print_r($return);
print_r(gettype($return)); //prints string
but i got the response in full string when the response supposed to be in xml format, and when i print the type of the response variable it prints string. I tried using Postman to check the response and it returns the correct format in XML mode:
and when i change to Preview mode in Postman, it showed the same result as what my CURL response in php shows, which is full string:
Upvotes: 6
Views: 3074
Reputation: 21465
most likely, you're getting the correct response, its just your browser not rendering it the way you expected it to. what happens if, at the top of your code, you add header("content-type: text/plain;charset=utf8");
? my guess, it will render the code the way you expected it to. alternatively, html encode it, like
function hhb_tohtml(string $str):string
{
return htmlentities($str, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE | ENT_DISALLOWED, 'UTF-8', true);
}
print_r(hhb_tohtml($return));
print_r(gettype($return)); //prints string
Upvotes: 6