Jump to content

sending a string as $_POST to another script


Confidence

Recommended Posts

Hi guys,

 

my goal is to send a string from one page (htmlsend.php) to another (htmlreceive.php)....the data should be sent as $_POST['nc'].......

 

if you wonder why, it a proof of concenpt, for more complex functionality as soon as this works.

 

i made some code, but it is not working properly....would be cool if someone helps me point out the error cause.

 

 

htmlsend.php

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');

$data='this is my data to send';

function do_post_request($url, $data, $optional_headers = null)
  {
 $params = array('http' => array(
			  'method' => 'POST',
			  'nc' => $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");
 }
 $response = @stream_get_contents($fp);
 if ($response === false) {
	throw new Exception("Problem reading data from $url");
 }
 return $response;
  }

echo do_post_request("http://localhost/jquery/htmlreceive.php", $data);

?>

 

htmlreceive.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo '<pre>';
print_r($_POST);
echo '</pre>';
$receive=$_POST['nc'];

echo 'incoming message '.$receive;

 

and this is the error i get when calling htmlsend.php

 

Array
(
)


Notice: Undefined index: nc in D:\Apache2\htdocs\jquery\htmlreceive.php on line 7
incoming message 

You might have to send out the request headers

 

   'header'=>
      "Accept-language: en\r\n".
      "Content-type: application/x-www-form-urlencoded\r\n",

 

For another implementation, you can visit http://www.jonasjohn.de/snippets/php/post-request.htm

 

-Kalivos

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.