PHP中的cURL在_FILE和_RETURNTRANSFER中返回不同的数据

时间:2022-10-18 20:40:48

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.

我注意到PHP中的cURL在被告知通过CURLOPT_FILE输出到文件时返回不同的数据,因为它被告知通过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.

_RETURNTRANSFER似乎剥离了换行符和额外的空格,就像解析它以显示为标准HTML代码一样。另一方面,_FILE完全按照预期保留文件。

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.

我已经阅读了php.net上的文档,但没有发现任何似乎可以解决我的问题。理想情况下,我希望_RETURNTRANSFER返回确切的内容,以便我可以删除一个中间文件,但我没有看到任何方法使这成为可能。

Here is the code I am using. The data in question is a CSV file with \r\n line endings.

这是我正在使用的代码。有问题的数据是带有\ r \ n行结尾的CSV文件。

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.

事实证明,错误不在于返回的内容,而在于我正在解析它的方式。 \ r \ n未按照我预期的方式解析单引号时,切换到双引号解决了我的问题。我不知道这在函数调用中有所不同。

This works just fine:$cresult = split("\r\n", $cresult);

这很好用:$ cresult = split(“\ r \ n”,$ cresult);

This does not: $cresult = split('\r\n', $cresult);

这不是:$ cresult = split('\ r \ n',$ cresult);

3 个解决方案

#1


1  

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.

事实证明,错误不在于返回的内容,而在于我正在解析它的方式。 \ r \ n未按照我预期的方式解析单引号时,切换到双引号解决了我的问题。我不知道这在函数调用中有所不同。

This works just fine:$cresult = split("\r\n", $cresult);

这很好用:$ cresult = split(“\ r \ n”,$ cresult);

This does not: $cresult = split('\r\n', $cresult);

这不是:$ cresult = split('\ r \ n',$ cresult);

#2


1  

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

在大多数脚本语言中(例如在Bash中也是如此),简单的引号用于表示写入时的内容,而双引号则被“分析”(我不认为这是合适的单词,但我找不到更好的单词)。

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

It is true for variables and escaped characters.

变量和转义字符都是如此。

#3


0  

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.

我没有尝试重现“bug”(我认为我们可以认为这是一个错误,如果它是实际的行为),但也许你可以克服它。

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.

PHP Doc说默认comportement是将结果写入文件,默认文件是STDOUT(浏览器的窗口)。你想要的是获得与文件相同的结果,但是在变量中。

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

你可以使用ob_start();和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 ;-)...)

(请原谅我英语不完美; - )...)

#1


1  

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.

事实证明,错误不在于返回的内容,而在于我正在解析它的方式。 \ r \ n未按照我预期的方式解析单引号时,切换到双引号解决了我的问题。我不知道这在函数调用中有所不同。

This works just fine:$cresult = split("\r\n", $cresult);

这很好用:$ cresult = split(“\ r \ n”,$ cresult);

This does not: $cresult = split('\r\n', $cresult);

这不是:$ cresult = split('\ r \ n',$ cresult);

#2


1  

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

在大多数脚本语言中(例如在Bash中也是如此),简单的引号用于表示写入时的内容,而双引号则被“分析”(我不认为这是合适的单词,但我找不到更好的单词)。

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

It is true for variables and escaped characters.

变量和转义字符都是如此。

#3


0  

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.

我没有尝试重现“bug”(我认为我们可以认为这是一个错误,如果它是实际的行为),但也许你可以克服它。

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.

PHP Doc说默认comportement是将结果写入文件,默认文件是STDOUT(浏览器的窗口)。你想要的是获得与文件相同的结果,但是在变量中。

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

你可以使用ob_start();和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 ;-)...)

(请原谅我英语不完美; - )...)