使用纯PHP生成http请求

使用纯PHP生成http请求,php,Php,我试图用自己的php代码发送http请求(我现在不想使用像CURL这样的外部库),但是我找不到正确的标记,或者我不知道如何使用$\u POST来实现这一目的。请提供任何帮助如果是GET请求,请使用file\u GET\u内容,也许您可以尝试以下方法: <?php $test = file_get_contents("http://example.com/"); var_dump($test); ?> <?php

我试图用自己的php代码发送http请求(我现在不想使用像
CURL
这样的外部库),但是我找不到正确的标记,或者我不知道如何使用
$\u POST
来实现这一目的。请提供任何帮助

如果是GET请求,请使用file\u GET\u内容,也许您可以尝试以下方法:

    <?php
        $test = file_get_contents("http://example.com/");
        var_dump($test);
    ?>
    <?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);
        }
    ?>

如果是POST请求,请使用fsock,或许您可以尝试以下方法:

    <?php
        $test = file_get_contents("http://example.com/");
        var_dump($test);
    ?>
    <?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);
        }
    ?>


这是否回答了您的问题?不,针对此问题给出的建议包括外部库。这是否回答了您的问题?你看到投票最多的答案了吗?1238票。它不使用外部库。我明白了,我没有注意到“少”部分。。。。但它仍然不能回答我的问题。。。。。。我正在尝试发布,我想建议[文件获取内容()]是获取请求。谢谢。这很有帮助。