Frage

I use this function to make my HTTP Requests and i successfully get the body of the Response:

function SendRequest($url, $method, $data, $headers){
    $context = stream_context_create(array
    (
        'http' => array(
            'method' => $method,
            'header' => $headers,
            'content' => $data
        )
    ));

    return file_get_contents($url, false, $context);
}

But i need a way to get the whole HTTP Response (including the headers)! Both for debugging and practical reasons.

For example lets say i make a request to login webservice and in the response there is a session cookie. This way i cannot get it.

*Also if possible, it would be nice if there was a way to log the Requests for debbuging.

War es hilfreich?

Lösung 2

I found out a way to do it by using $http_response_header.

function SendRequest($url, $method, $data, $headers) {
    $context = stream_context_create(array
    (
        'http' => array(
            'method' => $method,
            'header' => $headers,
            //'content' => http_build_query($data)
            'content' => $data
        )
    ));

    $response_body = file_get_contents($url, false, $context);
    //var_dump($http_response_header);
    return array(
        'headers' => $http_response_header, 
        'body' => $response_body
    ); 
}

*$http_response_header will be created in the local scope

Andere Tipps

Check out the stream_get_meta_data() function. For http streams, it will include the HTTP response headers and status line in the wrapper_data2

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top