PHP 如何发送原始 HTTP 数据包

发布于 2024-08-05 23:48:31 字数 94 浏览 6 评论 0原文

我想将原始 http 数据包发送到网络服务器并接收其响应,但我找不到方法来做到这一点。我对套接字缺乏经验,我发现的每个链接都使用套接字发送 udp 数据包。任何帮助都会很棒。

I want to send a raw http packet to a webserver and recieve its response but i cant find out a way to do it. im inexperianced with sockets and every link i find uses sockets to send udp packets. any help would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞取 2024-08-12 23:48:31

看一下 fsockopen 手册页中的这个简单示例

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

:服务器是通过fsockpen建立的。 $out 保存 HTTP 请求,然后使用 frwite 发送。然后使用 fgets 读取 HTTP 响应。

Take a look at this simple example from the fsockopen manual page:

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

The connection to the server is established with fsockpen. $out holds the HTTP request that’s then send with frwite. The HTTP response is then read with fgets.

霊感 2024-08-12 23:48:31

如果您只想执行 GET 请求并接收响应正文,则大多数文件函数都支持使用 url:

<?php

$html = file_get_contents('http://google.com');

?>

<?php

$fh = fopen('http://google.com', 'r');
while (!feof($fh)) {
    $html .= fread($fh);
}
fclose($fh);

?>

对于不仅仅是简单的 GET,请使用curl(您必须将其编译为 php)。使用curl,您可以执行POST 和HEAD 请求,以及设置各种标头。

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$html = curl_exec($ch);

?>

If all you want to do is perform a GET request and receive the body of the response, most of the file functions support using urls:

<?php

$html = file_get_contents('http://google.com');

?>

<?php

$fh = fopen('http://google.com', 'r');
while (!feof($fh)) {
    $html .= fread($fh);
}
fclose($fh);

?>

For more than simple GETs, use curl (you have to compile it into php). With curl you can do POST and HEAD requests, as well as set various headers.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$html = curl_exec($ch);

?>
红焚 2024-08-12 23:48:31

cURL 比实现客户端 HTTP 更容易。您所要做的就是设置一些选项,cURL 会处理其余的事情。

$curl = curl_init($URL);
curl_setopt_array($curl,
    array(
        CURLOPT_USERAGENT => 'Mozilla/5.0 (PLAYSTATION 3; 2.00)',
        CURLOPT_HTTPAUTH => CURLAUTH_ANY,
        CURLOPT_USERPWD => 'User:Password',
        CURLOPT_RETURNTRANSFER => True,
        CURLOPT_FOLLOWLOCATION => True
        // set CURLOPT_HEADER to True if you want headers in the result.
    )
);
$result = curl_exec($curl);

如果您需要设置 cURL 不支持的标头,请使用 CURLOPT_HTTPHEADER 选项,传递附加标头数组。如果需要解析标头,请将 CURLOPT_HEADERFUNCTION 设置为回调。阅读 curl_setopt 的文档以获得更多选择。

cURL is easier than implementing client side HTTP. All you have to do is set a few options and cURL handles the rest.

$curl = curl_init($URL);
curl_setopt_array($curl,
    array(
        CURLOPT_USERAGENT => 'Mozilla/5.0 (PLAYSTATION 3; 2.00)',
        CURLOPT_HTTPAUTH => CURLAUTH_ANY,
        CURLOPT_USERPWD => 'User:Password',
        CURLOPT_RETURNTRANSFER => True,
        CURLOPT_FOLLOWLOCATION => True
        // set CURLOPT_HEADER to True if you want headers in the result.
    )
);
$result = curl_exec($curl);

If you need to set a header that cURL doesn't support, use the CURLOPT_HTTPHEADER option, passing an array of additional headers. Set CURLOPT_HEADERFUNCTION to a callback if you need to parse headers. Read the docs for curl_setopt for more options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文