/    Sign up×
Community /Pin to ProfileBookmark

php curl input stream issue

The code below outputs the input stream

[code=php]<?php
$input = file_get_contents(“php://input”);

echo $input;
?>
<form method=”post” action=”test.php”>
<input type=”text” name=”test1″ value=””>
<input type=”text” name=”test2″ value=””>
<input type=”text” name=”test3″ value=””>
<input type=”submit” name=”submit” value=”Add”>
</form>[/code]

But when I use curl to post to the same script there is no input stream.

[code=php]$post_str = array(‘cmd’ => ‘check_user’);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “test.php”);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$res = curl_exec($ch);
curl_close($ch);

?>
[/code]

Anybody know why?

Thanks!

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@Maximus9999authorFeb 13.2010 — Any ideas on why there's no input stream through curl post?
Copy linkTweet thisAlerts:
@NogDogFeb 14.2010 — My guess is that it's because "test.php" is not a URL. You may need to use "http://localhost/test.php".

However, if the intent is to process a local file like that, it might be more efficient to simply populate $_POST with the the post data, then [b]require()[/b] the desired PHP file instead of getting the web server involved as a middleman with the extra processing that involves.
Copy linkTweet thisAlerts:
@Maximus9999authorFeb 14.2010 — Hi NogDog,

Thanks for the reply.

I apologize, the desired post is to another script on another server and not to a local file called test.php.

eg) http://domain.com/test.php

Calling a post locally to a file called test.php gives us stream data.

But when that script is called from another server there is no stream data.

Thanks and sorry for the confusion.
Copy linkTweet thisAlerts:
@NogDogFeb 14.2010 — Try something like this after the call to curl_exec():
[code=php]
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
}
[/code]
Copy linkTweet thisAlerts:
@Maximus9999authorFeb 15.2010 — Added the code, but there was no error.

The receiving test.php file contains the post data.

But $input = file_get_contents("php://input") contains nothing.

When I do a normal form post using html on the same server $input does contain the stream but not through curl. Not sure what's going on.
Copy linkTweet thisAlerts:
@NogDogFeb 15.2010 — I see you have CURLOPT_HEADER set, so I would think at the very least you would see some header data if it were connecting at all. ? Not real sure at this point what else to suggest. I assume you have PHP error_reporting set to E_ALL?
×

Success!

Help @Maximus9999 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...