使用PHP Curl,我如何准确查看发送的内容?

For instance, on the following CURL snippet:

  $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); //set target URL
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// allow redirects
    curl_setopt($ch, CURLOPT_POST, $usePost); // set POST method
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //set headers
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, $returnHeaders); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //prevent unverified SSL error

Before I run curl_exec on it, what if I want to see the full request headers and body before it is sent. (to see if is correctly following certain REST API guidelines)

You could send a request to the local server:

$test_url = 'http://localhost/nonexistent-page';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $test_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
// Other options.
curl_exec($ch);

echo nl2br(curl_getinfo($ch, CURLINFO_HEADER_OUT));

This will give you the request headers, with only the request line path and the Host: line being different from your actual request.

If you have access to a graphical environment on your server, you could use Wireshark to examine the network packets being sent and received. Wireshark allows you to use filters, to filter out specific IP-adresses and protocols.

For instance, I use this filter to see all the traffic from my cURL requests/responses to the server with IP w.x.y.z (substitute with the ip of the server you are connecting to):

ip.addr == w.x.y.z && http

I can then examine all my requests responses. This has given me great insight in what's happening 'under the hood'.