php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39034 Calling curl_exec() with return transfer returns bool(true)
Submitted: 2006-10-04 09:06 UTC Modified: 2006-10-10 23:17 UTC
From: james dot gauth at gmail dot com Assigned: iliaa (profile)
Status: Closed Package: cURL related
PHP Version: 4.4.4 OS: Debian
Private report: No CVE-ID: None
 [2006-10-04 09:06 UTC] james dot gauth at gmail dot com
Description:
------------
When calling curl_exec() with the CURLOPT_RETURNTRANSFER option set to true, a user expects that curl_exec() will return the string value of the request.

When the URL of the request in question points to a zero-byte file, curl_exec() returns bool(true) instead of the expected empty string.

The versions I am using are:
PHP 4.4.4 (cli) (built: Sep 11 2006 10:00:33)
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

CURL Information => libcurl/7.13.2 OpenSSL/0.9.7e zlib/1.2.3 libidn/0.5.13



Reproduce code:
---------------
<?php

	// fictional URL to an existing file with no data in it (ie. 0 byte file)
	$url = 'http://www.example.com/empty_file.txt';

	$curl = curl_init();
	
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_HEADER, false);

	// execute and return string (this should be an empty string '')
	$str = curl_exec($curl);

	curl_close($curl);

	// the value of $str is actually bool(true), not empty string ''
	var_dump($str);

?>

Expected result:
----------------
string(0) ""

Actual result:
--------------
bool(true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-05 11:58 UTC] james dot gauth at gmail dot com
The source of this problem is the following:

[ext/curl/interface.c][1664]

	if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) {
		--ch->uses;
		smart_str_0(&ch->handlers->write->buf);
		RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1);
	}
	--ch->uses;
	RETURN_TRUE;
}

If the write buffer has no length, the PHP_CURL_RETURN option is ignored and a value of TRUE is returned. This code has been in CVS since revision 1.1.

Regardless of the write buffer length, setting PHP_CURL_RETURN option on should case curl_exec() to return a string. If this code is not going to be changed I would recommend the PHP manual be changed to warn the user of this behaviour.

One of the issues this problem causes is that if you don't specifically check for the boolean true return value, any normal string comparisons made against curl_exec()'s return value are always true (true == 'string' is true) whereas the expected result would be ('' == 'string' is false).
 [2006-10-06 10:15 UTC] james dot gauth at gmail dot com
I'm setting the category back to cURL related, inconsistent return values are not a feature.
 [2006-10-10 23:17 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 07:01:29 2024 UTC