簡體   English   中英

使用php發送GET請求時出現HTTP 400錯誤請求錯誤

[英]HTTP 400 BAD REQUEST error while sending GET request with php

當我嘗試將GET請求發送到使用php的地址時,我收到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;

}

有什么建議么?

GET請求還包含標頭,其中包含諸如useragent或編碼之類的內容,您應該查看需要發送的內容和可選的內容

對於這個特定的問題,我找到了解決方案。 如果要從我在上述問題中發送的請求中獲取標頭,則可以使用php的get_headers($url)方法。 它檢索我一直在尋找的標頭。 我對協議和Web服務以及php(1或1.5周)真的很陌生,因此我可能問了一個愚蠢的問題,而且不夠具體。 無論如何,非常感謝您的回答。

祝你今天愉快!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM