Issue
I am making a request with CURL using PHP but I am receiving invalid response format in $result variable.
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$param=json_encode($params);
$post_string = '{"method": "'.$method.'", "params":'.$param.' , "id": "0"}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result='';
$result = curl_exec($ch);
echo $result;
and Result is as following
HTTP/1.1 200 OK Content-Type: application/json Date: Fri, 21 Feb 2020 07:11:50 GMT Content-Length: 36 {"result":[],"error":null,"id":"0"}
but expected is
{"result":[],"error":null,"id":"0"}
I have tried to fetch it with json_decode but it is not working.
Any Suggestion> Thanks in advance.
Solution
You explicitly asked for the headers to be included there.
https://www.php.net/manual/en/function.curl-setopt.php:
CURLOPT_HEADER - TRUE to include the header in the output.
Answered By - CBroe