4

In the first file - the file I execute I have the following content:

<?php   
$name = "Julia";
$article = "I like papers";
$url = "http://domain.com/process.php";
$param = array('http' => array(
          'method' => 'POST',
          'content' => $article

        ));

$mad = @stream_context_create($param);
$fp = @fopen($url, 'rb', false, $mad);
$response = @stream_get_contents($fp);
echo $response;
?>

in the second file http://domain.com/process.php I have this:

<?php
$name = $_POST["name"];
$article = $_POST["content"];
$article = $_POST["article"];

echo $article;
echo $name;
echo "Hello there</br>:\n";

?>

The output that I get is just:

 "Hello there"

So what is wrong, how do I pass the values $article and $name via the request and how to I extract them in the file process.php?

Brana
  • 1,197
  • 3
  • 17
  • 38

1 Answers1

6

You got it wrong at the content part, use http_build_query() to build the POST query.

$param = array( 'http' => array('method' => 'POST',
                                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                                'content' => http_build_query($data)
                                ));

And your post parameters should be like below, where the input name as key

$data['name'] = 'Julia';
$data['article'] = 'I like papers';

Personally, I will use curl.

frz3993
  • 1,595
  • 11
  • 13
  • Works thanks very much. Still I was not able to archive multithreading - but that is another question. – Brana Oct 17 '15 at 22:39
  • You mean simultaneous request? There is a thing called `curl_multi_init`. You can look that up – frz3993 Oct 18 '15 at 04:21
  • I tried this one as well, it works everywhere except on the server where I need it to work :) This should work as well because fopen should be asynhronius .. I just call 2 pages. – Brana Oct 18 '15 at 04:41
  • A good cURL PHP example: https://stackoverflow.com/questions/2138527/php-curl-and-http-post-example – Capripot Nov 27 '22 at 02:53