5

I am trying to communicate with an API using cURL Calls.

I am trying to see the exact request that I am sending to the API "including the header, the body and the fields that are being posted." How can I get a copy of the request?

I tried to use curl_getinfo but that does not tell me the exact request. Then, I have added a code to print the request to a file called request.txt

here is my code which is working fine but it is not showing the fields that are being posted

function CallAPI($method, $url, $data = false, $header = array())
{
    $curl = curl_init();

    if(!empty($header)){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    }
    $f = fopen('request.txt', 'w');
    //disable the use of cached connection
    curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE);

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

    curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
    curl_setopt($curl, CURLOPT_FILE, $f);
    curl_setopt($curl, CURLOPT_INFILESIZE, $f);
    curl_setopt($curl, CURLOPT_STDERR, $f);

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data){
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data){
                $url = sprintf("%s?%s", $url, http_build_query($data));
            }
    }   

    $result = curl_exec($curl);
    $info = curl_getinfo($curl);

    //print data
    echo '<pre>';
    print_r($info);
    echo '</pre>';
    curl_close($curl);

    fclose($f);

    return $result;
}

I am able to see something like this
* Hostname SERVERNAME was found in DNS cache
*   Trying INTERNAL IP...
* Connected to SERVERNAME (INTERNAL IP) port INTERNAL PORT (#0)
> POST /icws/connection HTTP/1.1
Host: SERVERNAME:INTERNAL PORT
Accept: */*
Accept-Language: en-US
Content-Length: 517
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------cb7489673677d758

* Done waiting for 100-continue
< HTTP/1.1 400 Bad Request
< Cache-Control: no-cache, no-store, must-revalidate
< Pragma: no-cache
< Expires: 0
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
< Content-Type: application/vnd.inin.icws+JSON; charset=utf-8
< Date: Mon, 04 May 2015 20:31:56 GMT
< Server: HttpPluginHost
< Content-Length: 79
* HTTP error before end of send, stop sending
< 

How can I also include the field that are being sent with the request?

Jaylen
  • 39,043
  • 40
  • 128
  • 221
  • possible duplicate of [How can I see the request headers made by curl when sending a request to the server?](http://stackoverflow.com/questions/866946/how-can-i-see-the-request-headers-made-by-curl-when-sending-a-request-to-the-ser) – Ejaz May 04 '15 at 20:11
  • 1
    I am asking on how to display the body not the header. My code display the header – Jaylen May 04 '15 at 20:13

0 Answers0