使用php发送GET请求时HTTP 400错误请求

使用php发送GET请求时HTTP 400错误请求,php,http,web,request,Php,Http,Web,Request,当我试图用php向地址发送GET请求时,我收到了htp400错误请求消息,我不知道为什么 这是我的密码: function redirect($url) { error_reporting(E_ERROR | E_PARSE); $components = parse_url($url); $port = $components["port"]; $ip = $components["host"]; $path = $components["path"

当我试图用
php
向地址发送
GET
请求时,我收到了
htp400错误请求
消息,我不知道为什么

这是我的密码:

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;

}

有什么建议吗?

GET请求还包含一个标头,其中包含诸如用户代理之类的内容。为了编码,您应该查看需要发送的内容以及可选的内容。对于这个特定问题,我找到了一个解决方案。如果您想从我在上述问题中发送的请求中获取标题,可以使用php的
get\u headers($url)
方法。它检索我正在查找的标题。我对协议和web服务以及php都是新手(1周或1.5周),因此我可能问了一个愚蠢的问题,而且不够具体。无论如何,非常感谢你的回答


祝你今天愉快

parse_url()
您的代码片段似乎不完整之后,我们可以看看
$path
是什么样子吗。很难推断出可能是什么问题。请提供“如何访问代码”以及更合适的代码。
$path
正确吗?我检查过了。例如
http://listen.radionomy.com/35x80
这是
/35x80
我这样做只是为了调用函数,它可以与一些URL一起工作:
重定向($argv[1])我用
http://radio1190.colorado.edu:8000/high.mp3
并且它使用
http://listen.radionomy.com/35x80
它给出了错误消息一些网站实际检查提供的useragent是否正确,所以这是我的第一个建议