How to view PHP cURL request body (e.g. CURLINFO_HEADER_OUT for headers)

I can view the headers of a request sent using php curl with the following:

curl_getinfo($ch, CURLINFO_HEADER_OUT);

I want to see the body of what is being sent, but life cannot find a way for me to do this.

+4
source share
3 answers

I could not find such an option after an intensive search for PHP cURL documentation.

My solution was to use Charles web proxy tool

Charles - HTTP--/HTTP-/ -, HTTP SSL/HTTPS . , HTTP ( cookie ).

+1

CURLOPT_HEADER true , . - :

$url = "https://www.example.com";
$ch = curl_init();

// Configure cURL handle
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);

$x = curl_exec($ch);

print "\nHeaders:\n";

// Get the out headers, explode into an array, and remove any empty string entries
$outHeaders = explode("\n", curl_getinfo($ch, CURLINFO_HEADER_OUT));
$outHeaders = array_filter($outHeaders, function($value) { return $value !== '' && $value !== ' ' && strlen($value) != 1; });
print_r($outHeaders);

// Seperate in headers from body of response
list($inHeaders, $content) = explode("\r\n\r\n", $x, 2);

// Break in headers into array and print_r them
$inHeaders = explode("\n", $inHeaders);
print_r($inHeaders);
-1

Try this feature:

function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
-2
source

Source: https://habr.com/ru/post/1620668/


All Articles