Page 1 of 1

POST and PHP

Posted: Mon Jun 16, 2008 12:26 pm
by Dj_Lord
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

Re: POST and PHP

Posted: Mon Jun 16, 2008 12:53 pm
by highjo
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[''].

Re: POST and PHP

Posted: Mon Jun 16, 2008 1:30 pm
by Dj_Lord
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?

Re: POST and PHP

Posted: Mon Jun 16, 2008 1:34 pm
by John Cartwright
Do you have curl installed? Check your phpinfo()

Re: POST and PHP

Posted: Mon Jun 16, 2008 1:42 pm
by Dj_Lord
how stupid of me.. curl wasn't installed. It is working now :D

Thanks ;)