0

I was trying to make my php code send http request with its own (I don't want to use external libraries like CURL for now),but I can't find the right tag or I don't know how to use $_POST for this purpose. Any help please

  • 1
    Does this answer your question? [How do I send a POST request with PHP?](https://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php) – Don't Panic May 06 '20 at 11:22
  • no,the suggestions given for this question include external libraries. – user13396528 May 06 '20 at 11:32
  • 1
    Does this answer your question? [How to make a post request without curl?](https://stackoverflow.com/questions/8596311/how-to-make-a-post-request-without-curl) – Dmitry Leiko May 06 '20 at 11:34
  • Did you see the top voted answer? 1238 votes. It does not use external libraries. – Don't Panic May 06 '20 at 11:35
  • I see, I didn't notice the 'less' part.... but it still doesn't answer my Q...... I am trying to post, I think the suggestion[file_get_contents() ] is GET request. – user13396528 May 06 '20 at 19:36
  • `I think the suggestion ... is GET request` - no, it isn't. – Don't Panic May 07 '20 at 01:40
  • ya you are right again..... I used it without assigning it to a variable and it solved my problems....... but how much secure is it? – user13396528 May 08 '20 at 04:13
  • 1
    Search and ye shall find. https://stackoverflow.com/questions/1008668/how-secure-is-a-http-post – Don't Panic May 09 '20 at 00:06

1 Answers1

0

If it's a GET request, using file_get_contents, maybe you can try something like this:

    <?php
        $test = file_get_contents("http://example.com/");
        var_dump($test);
    ?>

If it's a POST request, using fsock, maybe you can try something like this:

    <?php
        $domain = 'dummy.restapiexample.com';
        $fp = fsockopen($domain, 80);

        $vars = array(
            'name' => 'test',
            'salary' => '123',
            'age' => '23' 
        );
        $content = http_build_query($vars);

        fwrite($fp, "POST /api/v1/create HTTP/1.1\r\n");
        fwrite($fp, "Host: $domain\r\n");
        fwrite($fp, "Content-Type: application/json\r\n");
        fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
        fwrite($fp, "Connection: close\r\n");
        fwrite($fp, "\r\n");

        fwrite($fp, $content);

        header('Content-type: text/plain');
        while (!feof($fp)) {
            echo fgets($fp, 1024);
        }
    ?>