如何在执行之前打印出确切的cURL请求,包括已发布的字段

时间:2015-05-04 20:02:03

标签: php json api curl

我正在尝试使用cURL调用与API通信。

我正在尝试查看我发送给API的确切请求“包括标题,正文和正在发布的字段。”我怎样才能获得请求的副本?

我尝试使用curl_getinfo,但这并没有告诉我确切的要求。然后,我添加了一个代码来将请求打印到名为request.txt

的文件中

这是我的代码工作正常,但它没有显示正在发布的字段

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
< 

我如何还包括随请求一起发送的字段?

0 个答案:

没有答案