Question

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

Was it helpful?

Solution

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

OTHER TIPS

In most scripting langage (it's also true in Bash for instance), simple quotes are used to represent things as they are written, whereas double quotes are "analysed" (i don't think it's the appropriate word but i can't find better).

$str = 'foo';
echo '$str'; // print “$str” to the screen
echo "$str"; // print “foo” to the screen

It is true for variables and escaped characters.

I didn't try to reproduce the "bug" (I think we can consider this as a bug if it is the actual behavior), but maybe you could get over it.

The PHP Doc says that the default comportement is to write the result to a file, and that the default file is STDOUT (the browser's window). What you want is to get the same result than in a file but in a variable.

You could do that using ob_start(); and ob_get_clean();.

$ch = curl_init(...);
// ...
ob_start();
curl_exec($ch);
$yourResult = ob_get_clean();
curl_close($ch);

I know that's not really the clean way (if there is one), but at least it sould work fine.

(Please excuse me if my english is not perfect ;-)...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top