Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

77
Vistas
Same Http request works on one host but not the other

I have the following code:

function posturl($URL, $post_data, $referrer = "") {  
        $URL_Info = parse_url ( $URL );
        // Building referrer
        if ($referrer == "")
                $referrer = $_SERVER["SCRIPT_URI"];

        // making string from $data
        foreach ( $post_data as $key => $value )
                $values [] = "$key=" . urlencode ( $value );

        $data_string = implode ( "&", $values );

        if (! isset ( $URL_Info ["port"] ))
                $URL_Info ["port"] = 80;

        $request .= "POST " . $URL_Info ["path"] . " HTTP/1.1\n";
        $request .= "Host: " . $URL_Info ["host"] . "\n";
        $request .= "Referer: $referrer\n";
        $request .= "Content-type: application/x-www-form-urlencoded\n";
        $request .= "Content-length: " . strlen ( $data_string ) . "\n";
        $request .= "Connection: close\n";
        $request .= "\n";
        $request .= $data_string . "\n";

        echo ">>>> Request:\n";
        echo($request);
        var_dump($URL_Info);

        $fp = fsockopen ( $URL_Info ["host"], $URL_Info ["port"] );
        fputs ( $fp, $request );
        while ( ! feof ( $fp ) ) {
                $result .= fgets ( $fp, 128 );
        }
        fclose ( $fp );
        $result=substr($result, strpos($result, "\r\n\r\n"));
        return $result;
}

And the following request:

POST /fia/interface/product_use_action.php HTTP/1.1
Host: orlof.is
Referer: 
Content-type: application/x-www-form-urlencoded
Content-length: 111
Connection: close

task=check&ticket_number=EDDAFIA073&secret_code=htl440&seller_number=104&ternimal_number=Edda&global_language=8

I get the following response:

https://pastebin.com/5sf4fUiY

My question:

Is there something wrong with the request?

Also I have the same code and the same request but different Host, and I don't get errors there.

POST /fia/interface/product_use_action.php HTTP/1.1
Host: test.orlof.is
Referer: 
Content-type: application/x-www-form-urlencoded
Content-length: 111
Connection: close

task=check&ticket_number=EDDAFIA073&secret_code=htl440&seller_number=104&ternimal_number=Edda&global_language=8

As we can see the only difference is test.orlof.is instead of orlof.is.

Thanks.

9 months ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I'm not sure if this is really the problem, but there are two obviously wrong things with your request:

    $request .= "POST " . $URL_Info ["path"] . " HTTP/1.1\n";

The end of line with HTTP should be \r\n, not \n. This is true for all line endings including the separator between HTTP header and body.

   $request .= "Content-length: " . strlen ( $data_string ) . "\n";
   ...
   $request .= $data_string . "\n";

You specify the length of the body with strlen ( $data_string ) but you actually send one byte more. There is no need to send a \n at the end of the body but when you do you should account for this in the content-length header too.

Apart from that you are doing a HTTP/1.1 request but expect that you can simply treat the HTTP response body as the final data. But, with HTTP/1.1 the server can only used chunked transfer encoding, i.e. the response will be in chunks with each chunk having a size prefix. If you don't want to deal with this better use HTTP/1.0.

As to why it works with one server and not the other: it might be because one server might have a more forgiving implementation of the HTTP standard and the other a more strict. In any case the ultimate source for how use HTTP is the standard and not some examples you've seen somewhere.

9 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos