【问题标题】:HTTP 400 BAD REQUEST error while sending GET request with php使用 php 发送 GET 请求时出现 HTTP 400 BAD REQUEST 错误
【发布时间】: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;

}

有什么建议吗?

【问题讨论】:

  • 我们能看看$pathparse_url()之后的样子吗
  • 您的代码 sn-p 似乎不完整。很难推断可能是什么问题。请提供“您如何访问代码”以及更合适的代码。
  • $path 是正确的,我检查了它。例如http://listen.radionomy.com/35x80/35x80
  • 我这样做只是为了调用该函数,它适用于一些 URL:redirect($argv[1]);

标签: php http web request


【解决方案1】:

GET 请求还包含一个标头,其中包括诸如用户代理或编码之类的内容,您应该看看您需要发送什么以及哪些是可选的

【讨论】:

  • 我用http://radio1190.colorado.edu:8000/high.mp3 尝试了相同的代码,它打印了标题但是用http://listen.radionomy.com/35x80 它给出了错误消息
  • 一些网站实际上会检查提供的用户代理是否正确,所以这是我的第一个建议
【解决方案2】:

对于这个特定的问题,我找到了解决方案。如果您想从我在上述问题中发送的请求中获取标头,您可以使用 php 的 get_headers($url) 方法。它检索我正在寻找的标题。我对协议和网络服务以及 php(1 或 1.5 周)真的很陌生,因此我可能问了一个愚蠢的问题并且不够具体。无论如何,非常感谢您的回答。

祝你有美好的一天!

【讨论】:

    猜你喜欢
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2021-08-29
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多