PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Dj_Lord
Forum Newbie
Posts: 6 Joined: Mon Jun 16, 2008 11:51 am
Post
by Dj_Lord » Mon Jun 16, 2008 12:26 pm
Hello,
I'd like to post some data and get the result. Problem is that I have to post to remote server. Here is what i've got:
Code: Select all
//================
//first.php
//================
<?php
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'post',
'content' => $data
));
if ($optional_headers!== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
echo do_post_request("localhost/test.php", "name1=value1&name2=value2") ;
?>
Code: Select all
//=============
//test.php
//================
<?php
print_r($_POST);
echo "muu";
?>
after running first.php i get: Array()muu.
Why $_Post array is empty?
Thank you,
Dj_Lord
highjo
Forum Contributor
Posts: 118 Joined: Tue Oct 24, 2006 1:07 pm
Post
by highjo » Mon Jun 16, 2008 12:53 pm
you might want to use a proper tool for posting to an url, using curl. i've encourtered some problem sing it before and found the solution here.so just give a try.see here
viewtopic.php?f=1&t=82783 . actually the last suggestion is a good one.but you have to set the full path like
.
the second point is just you have to collect post data with those global variables either $_REQUEST[''] or $_POST[''].
Dj_Lord
Forum Newbie
Posts: 6 Joined: Mon Jun 16, 2008 11:51 am
Post
by Dj_Lord » Mon Jun 16, 2008 1:30 pm
strange..
debugging gives correct results.. IE gives internal server error 500. Opera: "check point 1" when code is:
Code: Select all
<?php
echo "check point 1";
$ch = curl_init();
echo "check point 2";
curl_setopt($ch, CURLOPT_URL, "http://localhost/test.php");
$data = array("name" => "string", "name2" => "string2",);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
echo $output;
?>
do you know why?
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Mon Jun 16, 2008 1:34 pm
Do you have curl installed? Check your phpinfo()
Dj_Lord
Forum Newbie
Posts: 6 Joined: Mon Jun 16, 2008 11:51 am
Post
by Dj_Lord » Mon Jun 16, 2008 1:42 pm
how stupid of me.. curl wasn't installed. It is working now
Thanks