【发布时间】:2014-08-22 03:28:12
【问题描述】:
当我尝试向带有php 的地址发送GET 请求时,我收到了HTTP 400 BAD REQUEST 消息,但我不知道为什么。
这是我的代码:
function redirect($url)
{
error_reporting(E_ERROR | E_PARSE);
$components = parse_url($url);
$port = $components["port"];
$ip = $components["host"];
$path = $components["path"];
//create and connect socket with the parameters entered by the user
//$sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
echo "Establishing connection to the given adress...\n";
//Connection timeout limit is set to 10 seconds...
if(!isset($port))
{
$port = 80;
}
$sock = fsockopen($ip, $port,$errno, $errstr, 10) or die("Unable to connect...");
$request = "GET $path HTTP/1.1" . "\r\n\r\n";
fwrite($sock, $request);
while ($header = stream_get_line($sock, 1024, "\r\n")) {
$response.= $header . "\n";
}
echo $response;
$loc = "";
if (preg_match("/Location:\'(.*)\\n/", $response, $results))
$loc = $results[1];
echo $loc;
}
有什么建议吗?
【问题讨论】:
-
我们能看看
$path在parse_url()之后的样子吗 -
您的代码 sn-p 似乎不完整。很难推断可能是什么问题。请提供“您如何访问代码”以及更合适的代码。
-
$path是正确的,我检查了它。例如http://listen.radionomy.com/35x80是/35x80 -
我这样做只是为了调用该函数,它适用于一些 URL:
redirect($argv[1]);