0

I'm having an issue with an API saying it get the wrong data, but when I look at what I send to curl it seems to be perfectly fine.

But I can't find a way to see exactly what cURL is sending. I can only figure out how to see the headers, not the body of the request I'm sending.

Does anyone know how I can see the actual raw data, including the body of the request that is sent? While not really necessary, I included my code below for good measure.

If found a few similar questions on stackoverflow, but they were all unanswered/solved in other ways.

    private function createCurl( $url, $http_context ) {
    $ch = curl_init( $url );

    $curl_opts = array(
        CURLOPT_HEADER         => false, 
        CURLOPT_RETURNTRANSFER => true, 
        CURLOPT_SSL_VERIFYPEER => false
    );

    $curl_opts[ CURLOPT_CUSTOMREQUEST ] = $http_context['method'];

    if ( ! empty( $http_context['content'] ) ) {
        $curl_opts[ CURLOPT_POSTFIELDS ] =& $http_context['content'];
    }

    $curl_opts[ CURLOPT_HTTPHEADER ] = array_map( 'trim', explode( "\n", $http_context['header'] ) );

    if ( ! empty( $this->curlOptions ) && is_array( $this->curlOptions ) ) {
        $curl_opts = array_merge( $curl_opts, $this->curlOptions );
    }

    curl_setopt_array( $ch, $curl_opts );

    return $ch;
}

private static function &execCurlAndClose( $ch, &$out_response_headers = null ) {
    if ( is_array( $out_response_headers ) ) {
        self::$_curlHeadersRef =& $out_response_headers;
        curl_setopt( $ch, CURLOPT_HEADERFUNCTION, array( __CLASS__, '_curlHeaderCallback' ) );
    }
    $res     = curl_exec( $ch );
    $err_no  = curl_errno( $ch );
    $err_str = curl_error( $ch );
    curl_close( $ch );

    return $res;
}
James T
  • 99
  • 6
  • 1
    Have a look through https://stackoverflow.com/questions/9707551/getting-content-body-from-http-post-using-php-curl see if it helps. – Nigel Ren Mar 06 '21 at 15:32
  • Thanks, but like the other posts, it seems to only provide a solution to viewing the response, not the request. Seems like it's not possible for some reason. I guess a port sniffer is the way to go... – James T Mar 06 '21 at 15:39
  • 1
    It suggests that CURLOPT_VERBOSE is the way to do it, but not something I've tried. – Nigel Ren Mar 06 '21 at 15:41
  • Does this answer your question? [How can I see the request headers made by curl when sending a request to the server?](https://stackoverflow.com/questions/866946/how-can-i-see-the-request-headers-made-by-curl-when-sending-a-request-to-the-ser) – Progman Mar 06 '21 at 16:08
  • @Progman Unfortunately that is only the headers, which is no problem to get. It's the body that seems to be impossible to see. – James T Mar 08 '21 at 09:48

1 Answers1

0

Use --trace logfilename or --trace-ascii logfilename option

DimaA6_ABC
  • 578
  • 4
  • 15