0

I want to catch with "file_get_contents" or some other method the result of a combined GET/POST request in a file.

The URL already contains some GET Variables like for example

https://www.example.com/?var1=val1&$var2=val2

and I want to catch the result of a POST like

<form action="https://www.example.com/?var1=val1&$var2=val2">
<input type="hidden" name="var3" value="val3">
<input type="submit" name="var4" value="val4">
</form>

can someone please give some hints ? or point to some documentation?

UPDATE: (my english is also not so good) this is why I will go on with asking in a more complex way.

Bart shows here a example how file_get_contents can make a post. What I am missing is how to also send the POST vars (text/hidden/radio/submit) in this example. Hope now its more clear.

  • `$_GET` provides GET variables, `$_POST` provides POSTed variables. You can also use `$_SERVER` for server variables including URL, path, request method, etc. You can access both in the same script. – JonJ Dec 16 '18 at 16:04
  • Are you asking how to use [curl](http://php.net/manual/en/function.curl-init.php) (that's the normal way to emit a http request)? – AD7six Dec 16 '18 at 16:05
  • I don't think "copy the docs into an answer for me" is appropriate @HenryStack. There are also [at least eight thousand similar questions](https://stackoverflow.com/search?q=php+curl+post) already – AD7six Dec 16 '18 at 16:11
  • @JonJ, thanks for the hint. I want to catch the HTML code of that request in a file. With file_get_contents or curl or another method. –  Dec 16 '18 at 16:12
  • Are the files on your server? You could use `include` and set the `POST` and `GET` manually prior to the include – user3783243 Dec 16 '18 at 16:13
  • @user3783243 they are not. I want to read html code in a file –  Dec 16 '18 at 16:23
  • You will need to use CURL in that case. Maybe this is a good thread, https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code – user3783243 Dec 16 '18 at 16:36

2 Answers2

2

It is easy to catch both get and post.

// from the querystring, GET
$var1 = $_GET['var1']
$var2 = $_GET['var2']

// from the posted variables POST
$var3 = $_POST['var3']
$var4 = $_POST['var4']

Because I’m Korean middle school student, I can misunderstand your question. If my answer is wrong, please reply to me

RiggsFolly
  • 83,545
  • 20
  • 96
  • 136
Uhm Seohun
  • 31
  • 5
0

I found the answer in a sniplet from Mr. Martini: you must use http_build_query to build the variables array and feed it in the opts array.

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);