I am encountering an issue where once curl is explicitly set to use CURLOPT_HEADER, all the responses I get from a REST Endpoint (SugarCRM 7) will include the full HTTP Header. I did this so I can get and store Cookies for session management, but the immediate downside is the response I receive from curl_exec() is that it includes all headers, effectively breaking any attempt to interpret the returned result as valid JSON. The actual response from a POST call is posted below.
"HTTP/1.1 200 OK
Date: Thu, 25 Feb 2016 03:23:35 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips mod_fcgid/2.3.9 PHP/5.4.16
X-Powered-By: PHP/5.4.16
Set-Cookie: PHPSESSID=f0bsbjelfa9c0ada7qj1816986; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 75
Content-Type: application/json; charset=UTF-8
{"examplekey":"examplevalue"}"
If CURLOPT_HEADER is disabled, the response sends a valid JSON object, but then there is no effective way to track the life of the session. It should be fairly predictable to pull the JSON data out through splitting across CRLFs, but there has to be a way to have cURL send both the body response, and the headers (cookies and all) without having to take a risk with regexing CRLFs with the whole response. Unfortunately, I have had no luck accessing cookies without CURLOPT_HEADER disabled.
To summarize -- what would be the best approach to preserve the headers from a cURL request, while leaving a valid JSON-serialized String that I can process?
Below is the method being used for this process.
public function RunCurl() {
ob_start();
//Set x-www-form-urlencoded header
$Headers[] = 'Content-Type: application/x-www-form-urlencoded';
$Headers[] = 'cache-control: no-cache';
curl_setopt($this->CurlRequest, CURLOPT_VERBOSE, true);
curl_setopt($this->CurlRequest, CURLOPT_URL, $this->SourceURL);
curl_setopt($this->CurlRequest, CURLOPT_HEADER, 1);
curl_setopt($this->CurlRequest, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->CurlRequest, CURLOPT_POSTFIELDS, http_build_query($this->EncapsulatedPostData));
curl_setopt($this->CurlRequest, CURLOPT_HEADER, 1);
curl_setopt($this->CurlRequest, CURLOPT_HTTPHEADER, $this->Cookie);
try {
$Results = curl_exec($this->CurlRequest);
preg_match_all('/^Set-Cookie:\s*([^\r\n]*)/mi', $Results, $ms);
$this->Cookie = $ms;
curl_close($this->CurlRequest);
} catch (Exception $ex) {
echo 'Caught exception: ', $ex->getMessage(), "\n";
return false;
}
ob_end_flush();
return json_decode(utf8_decode($Results));
}