我正在使用以下脚本向网站发送GET请求,但是,我不断收到错误,服务器没有响应。PHP套接字GET请求
- 我已经运行wireshark查看由于运行此脚本而产生的任何流量,但没有流量。
- 我可以从浏览器连接到网站,我也可以通过使用file_get_contents函数检索内容,但是使用套接字,我无法做到这一点。
下面是使用套接字将请求发送到站点的代码,如果连接不成功,它将返回错误:“服务器没有响应”。
<?php
error_reporting(0);
set_time_limit(0);
ini_set("default_socket_timeout", 5);
function http_send($host, $packet)
{
if (!($sock = fsockopen($host, 80)))
die("\n[-] No response from {$host}:80\n");
fwrite($sock, $packet);
return stream_get_contents($sock);
}
$host="http://example.com";
$path="/";
$packet = "GET {$path} HTTP/1.0\r\n";
$packet .= "Host: {$host}\r\n";
$packet .= "Connection: close\r\n\r\n";
http_send($host, $packet);
?>
那么,上面的代码有什么问题,它无法发送GET请求?
对我来说看起来没问题,它会为GET请求形成HTTP请求头并存储在$ packet变量中。
$ host是目的站点名称。
然后它调用将发送请求的函数http_send。
错误输出:[ - ]被返回由fsockopen
在误差从http://example.com:80
为什么使用套接字而不是file_get_contents和stream_context? – grunk