php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #15279 RETURNTRANSFER and CUSTOMREQUST do not play well together
Submitted: 2002-01-29 11:53 UTC Modified: 2002-02-08 21:00 UTC
From: robert at gonkgonk dot com Assigned: torben (profile)
Status: Closed Package: Documentation problem
PHP Version: 4.1.1 OS: Linux
Private report: No CVE-ID: None
 [2002-01-29 11:53 UTC] robert at gonkgonk dot com
When using curl to request a page, if I just set RETURNTRANSFER curlopt, things work as expected (output is dumped into a variable). If I just set CUSTOMREQUEST, thinks work as expected (output is dumped to stdout). However, if I set RETURNTRANSFER and CUSTOMREQUEST at the same time, I get no output. The request is made, according to my apache logs, but I get nothing. Here is some demo code:

<?

$c = curl_init();

$request = '';

$request .= 'GET /testfiles/phptest.php' . "\r\n";

curl_setopt($c, CURLOPT_URL,
     'http://localhost:8080/testfiles/phptest.php');
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $request);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

echo trim( curl_exec($c) );

curl_close($c);

?>

That is an example of a request that will not work. Comment either one of the "curl_setopt" lines out and it will.

Thanks,

robert

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-02-08 16:26 UTC] torben@php.net
What happens if you try with a valid HTTP request, or just 
'GET', as the custom response? The script you gave below 
didn't work for me either, but did if I did either of:

  $request = "GET /index.html HTTP/1.0\r\n\r\n";

...or...

  $request = "GET";


Can you try that and report back?


Torben
 [2002-02-08 16:42 UTC] robert at gonkgonk dot com
Sure. Here are some very simple runs and results. I do not think that any request will ever work.

----- START

----- SRC

<?

	$curl = curl_init();

	$request = 'GET' . "\r\n";
	
	curl_setopt($curl, CURLOPT_URL, 'http://localhost:8080/');

	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request);

	$data = curl_exec ($curl);
	
	echo $data . "\r\n";

?>

----- RESULT

[BLANK]

----- END

----- START

----- SRC

<?

	$curl = curl_init();

	$request = 'GET' . "\r\n";
	
	curl_setopt($curl, CURLOPT_URL, 'http://localhost:8080/');

	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

	$data = curl_exec ($curl);
	
	echo $data . "\r\n";

?>

----- RESULT

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>403 Forbidden</TITLE>
</HEAD><BODY>
<H1>Forbidden</H1>
You don't have permission to access /
on this server.<P>
<HR>
<ADDRESS>Apache/1.3.22 Server at 127.0.0.1 Port 8080</ADDRESS>
</BODY></HTML>

----- END

----- START

----- SRC

<?

	$curl = curl_init();

	$request = 'GET' . "\r\n";
	
	curl_setopt($curl, CURLOPT_URL, 'http://localhost:8080/');

	curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request);

	$data = curl_exec ($curl);

?>

----- RESULT

<HTML><HEAD>
<TITLE>403 Forbidden</TITLE>
</HEAD><BODY>
<H1>Forbidden</H1>
You don't have permission to access /
on this server.<P>
<HR>
<ADDRESS>Apache/1.3.22 Server at 127.0.0.1 Port 8080</ADDRESS>
</BODY></HTML>

----- END

 [2002-02-08 17:01 UTC] torben@php.net
Well, in the first place, the results below show that it
did in fact work for you, but that your server isn't set
up to provide a directory listing of the root dir and 
also doesn't have an index file there.

A bit more research shows that valid values for 
CURLOPT_CUSTOMREQUEST are things like 'GET', 'HEAD', 
'DELETE', and so on. You should not enter the whole request 
line, and you should not enter any newlines/carriage returns.

If the following script works for you, I will reclassify
this as a documentation problem.

<?php
error_reporting(E_ALL);

$c = curl_init();

$request = "GET";

curl_setopt($c, CURLOPT_URL, 'http://localhost:8080/testfiles/phptest.php');
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $request);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

echo trim(curl_exec($c));

curl_close($c);

?>


Torben
 [2002-02-08 17:08 UTC] torben@php.net
Scratch the first paragraph of my last post. :)

Torben
 [2002-02-08 20:54 UTC] robert at gonkgonk dot com
Yes, actually, that is what I would have expected it to return, since my machine doesn't have any index file in the root director. Only, I would have expected it in all three cases, but I guess you caught that :)

Anyhow, it appears you are right, and that I am misusing the CUSTOMREQUEST option. Your example executed as expected. I looked at the curl_easy_setopt manpage and confirmed this (why I had not caught this before from the man page is a good question).

The strange thing is that I have been using CUSTOMREQUEST on a machine running 4.0.6 to post XML in a fashion such as this:

$xml = '';
$xml .= "POST /somepath HTTP/1.1\r\n";
$xml .= "Host: somehost\r\n";
$xml .= "Content-Type: text/xml\r\n";
$xml .= "\r\n"
$xml .= $xmldoc;

...
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request);
...

Anyhow, I feel really silly now; I should have seen this answer long before now. But at least I know why I haven't been able to duplicate the behavior of the one machine that is working, though why it is working is an interesting question.

Thanks,

robert
 [2002-02-08 21:00 UTC] torben@php.net
Cool--I've already updated the manual to mention that this
is the way that option should be used, so I'm gonna close
this bug now.

And yeah, I've not looked into why it will sometimes work
with the whole request shoved in there, but it sometimes
does and sometimes doesn't, so there you go.


Thanks for the report,

Torben
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Nov 27 04:01:27 2024 UTC