POST and PHP

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

Post Reply
Dj_Lord
Forum Newbie
Posts: 6
Joined: Mon Jun 16, 2008 11:51 am

POST and PHP

Post 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
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: POST and PHP

Post 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[''].
Dj_Lord
Forum Newbie
Posts: 6
Joined: Mon Jun 16, 2008 11:51 am

Re: POST and PHP

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: POST and PHP

Post by John Cartwright »

Do you have curl installed? Check your phpinfo()
Dj_Lord
Forum Newbie
Posts: 6
Joined: Mon Jun 16, 2008 11:51 am

Re: POST and PHP

Post by Dj_Lord »

how stupid of me.. curl wasn't installed. It is working now :D

Thanks ;)
Post Reply