dsfw2154 2014-07-01 14:14
浏览 690
已采纳

使用php发送GET请求时出现HTTP 400 BAD REQUEST错误

While I'm trying to send a GET request to an address with php I receive HTTP 400 BAD REQUEST message and I can't figure out why.

Here's my code:

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...
";


    //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" . "

";


    fwrite($sock, $request);

    while ($header = stream_get_line($sock, 1024, "
")) {
        $response.= $header . "
";
    }
    echo $response;
    $loc = "";

    if (preg_match("/Location:\'(.*)\
/", $response, $results))
        $loc = $results[1];

    echo $loc;

}

Any suggestions?

  • 写回答

2条回答 默认 最新

  • drpzr64329 2014-07-01 15:47
    关注

    For this specific problem I've found a solution. If you want to get the headers from the request that I sent in the question above, you can use php's get_headers($url) method. It retrieves the headers that I was looking for. I'm really new to protocols and web-services and also to php (1 or 1.5 weeks), therefore I may have asked a silly question and not have been specific enough. Anyways thank you very much for your answers.

    Have a nice day!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器