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

Php 如何捕获HTTP请求的普通副本,php,json,http,curl,icws,Php,Json,Http,Curl,Icws,我正在尝试使用cURL和PHP与anrestapi通信。但是,我在API中遇到了一个问题,我需要能够以纯文本形式查看我的HTTP请求,以便对其进行分析和更正 我尝试了以下方法将我的请求写入文件 $file = fopen('request.txt', 'w'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_VERBOSE, true);

我正在尝试使用cURL和PHP与anrestapi通信。但是,我在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
如何以纯文本形式查看整个HTTP请求?

您可以使用Fiddler()以纯文本形式查看完整的请求/响应,并测试REST API

此外,还可以使用Wireshark()应用HTTP筛选器

我的建议是Fiddler,我经常使用它来测试和调试我的RESTAPI


希望这能帮助您获得HTTP 500错误响应。你确定你的应用程序在出现内部错误的情况下提供了一个带有JSON的主体吗?是的,我得到了一个HTTP 500内部错误。这是来自API的响应,当我查看API日志时,我得到了“thisJSONlib::Value不是eValueType_数组类型”,因此我想查看纯文本,看看json是否具有正确的数据我如何使用Fiddler?我是在apache服务器、PC还是API服务器上安装它?在哪个部分我可以看到我的请求的纯文本?请注意,我的Apache服务器是HTTPS,但我只在HTTP上发送请求。您必须将其安装在执行请求的客户端计算机上。好的,这就是我所做的。我确实在左边看到了请求,但在哪里可以找到纯文本的请求?双击请求。在屏幕右侧,您将屏幕分为2部分(顶部和底部)。顶部是您的完整请求,底部是您的完整响应。在选项卡(顶部和底部)中,您可以选择“原始”。这里有您的原始请求(顶部)和原始响应(底部)。我不确定我遗漏了什么。我点击了左窗格中的请求。然后在右边的“顶部”点击“文本视图”选项卡。但结果似乎是加密的。在JSON选项卡中,我只看到2个节点(即
0
x17
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;
}