I have noticed that cURL in PHP returns different data when told to output to a file via CURLOPT_FILE
as it does when told to send the output to a string via CURLOPT_RETURNTRANSFER
.
_RETURNTRANSFER
seems to strip newlines and extra white space as if parsing it for display as standard HTML code. _FILE
on the other hand preserves the file exactly as it was intended.
I have read through the documentation on php.net but haven't found anything that seems to solve my problem. Ideally, I would like to have _RETURNTRANSFER
return the exact contents so I could eliminate an intermediate file, but I don't see any way of making this possible.
Here is the code I am using. The data in question is a CSV file with \r\n line endings.
function update_roster() {
$url = "http://example.com/";
$userID = "xx";
$apikey = "xxx";
$passfields = "userID=$userID&apikey=$apikey";
$file = fopen("roster.csv","w+");
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $passfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $file);
$variable_in_question = curl_exec ($ch);
curl_close ($ch);
fclose($file);
return $variable_in_question;
}
Turns out, the error is not in what was being returned, but in the way I was going about parsing it. \r\n is not parsed the way I expected when put in single quotes, switching to double quotes solved my problem. I was not aware that this made a difference inside function calls like that.
This works just fine:$cresult = split("\r\n", $cresult);
This does not: $cresult = split('\r\n', $cresult);