如何捕获我的 HTTP 请求的普通副本

How to capture a plain copy of my HTTP request

我正在尝试使用 cURL 和 PHP 与 REST API 通信。但是,我遇到了 API 的问题,我需要能够以纯文本形式查看我的 HTTP 请求,以便我可以分析并更正它。

我尝试了以下方法将我的请求写入文件

        $file = fopen('request.txt', 'w');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        curl_setopt($ch, CURLOPT_VERBOSE, true);    
        curl_setopt($ch, CURLOPT_STDERR, $file);        

但这并没有显示 JSON 数据。

这是我在 request.txt 文件中得到的内容

* Hostname servername was found in DNS cache
*   Trying INTERNAL IP...
* Connected to servername (INTERNAL IP) port 8018 (#0)
> PUT /icws/1163386002/messaging/subscriptions/queues/blahblah HTTP/1.1
Host: servername:8018
Accept: */*
ININ-ICWS-CSRF-Token: WAhtYWxoYXlla1dBY2GHSDMNDFtjMDcwZmIyYy1mguc4LTQ3YjEtODgzMy1iOTBkM2ZhYWHfoyNmYTlYCjEwLjAuNC4xNjA=
ININ-ICWS-Session-ID: 1163386002
Cookie: icws_1163386002=232sggsdfgdabe-404c-8d8c-12345dfgdfg
Content-Length: 156
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 156 out of 156 bytes
< HTTP/1.1 500 Internal Server Error
< 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: Tue, 12 May 2015 17:52:52 GMT
< Server: HttpPluginHost
< Content-Length: 90
< 
* Connection #0 to host servername left intact

我想以纯文本形式查看 JSON 字符串。

下面是我使用 PHP

进行 cURL 调用的方式
private function _processRequest($method, $uri, $data = false, $header = NULL, &$httpRespond = array(), &$httpCode = NULL)
{
    $ch = curl_init();
    $url = $this->_baseURL . $uri;

    if( 
           ($method == 'POST' || $method == 'PUT') 
        && $data
    ){
        $jsonString = json_encode( $data );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $jsonString );
    }

    if($method == 'POST'){
        curl_setopt($ch, CURLOPT_POST, true);
    } elseif( $method == 'PUT'){
        //curl_setopt($ch, CURLOPT_PUT, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    } else {
        if ($data){
            $url = sprintf("%s?%s", $url, http_build_query($data));
        }
    }   

    //disable the use of cached connection
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

    //return the respond from the API
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //return the HEADER respond from the API
    curl_setopt($ch, CURLOPT_HEADER, true);

    //add custom headers
    if(!empty($header)){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }

    //enable SSL
    if( $this->_protocol == 'https' ){
        curl_setopt($ch, CURLOPT_CAINFO, $this->_cainfo);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
    }

    //set the URL
    curl_setopt($ch, CURLOPT_URL, $url);

        $file = fopen('request.txt', 'w');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        curl_setopt($ch, CURLOPT_VERBOSE, true);    
        curl_setopt($ch, CURLOPT_STDERR, $file);        


    $respond = curl_exec($ch);

    //throw cURL exception
    if($respond === false){
        $errorNo = curl_errno($ch);
        $errorMessage = curl_error($ch);

        throw new ApiException($errorMessage, $errorNo);
    }   

    list($header, $body) = explode("\r\n\r\n", $respond, 2);

    $httpRespond = $this->_http_parse_headers($header);


    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  

    $result = json_decode($body, true);

    return $result;
}

如何以纯文本形式查看整个 HTTP 请求?

您可以使用 Fiddler (http://www.telerik.com/fiddler) 以纯文本形式查看完整的 request/response 并测试您的 REST API。

此外,您可以使用 Wireshark (https://www.wireshark.org/) 应用 HTTP 过滤器。

我推荐的是 Fiddler,我经常使用它来测试和调试我的 REST API。

希望对您有所帮助