缺少cURL响应体(数据未显示)

时间:2015-08-25 15:31:48

标签: php api http post curl

我正在使用CakePHP 2.4.x上的特定restful API,API的一部分包括通过curl POST / PUT发送文件,我们无法在CakePHP中复制我们需要的内容' s HttpSocket因此选择了传统的卷曲。尝试使用PHP的curl命令根本不会输出任何内容(尽管暂停显示它们正在运行)。

设置shell_exec将输出信息,但遗憾的是我们没有获得对保存数据或查看特定错误消息很重要的响应主体。

我添加了我认为与此问题相关的所有内容(我在OSX上,本地环境XAMPP)。我的cURL版本打印:

curl 7.30.0 (x86_64-apple-darwin13.0) libcurl/7.30.0 SecureTransport zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp 
Features: AsynchDNS GSS-Negotiate IPv6 Largefile NTLM NTLM_WB SSL libz

弃用的PHP代码(打印string(0) ""

    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_HTTPHEADER, Array(
        "Authorization: Bearer {$authInfo->access_token}",
        'Accept: application/json'
    ));

    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    $post[$data['file']['name']] = '@' . $data['file']['location'];
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_URL, $url);
    $response = curl_exec($ch);

    pr($post); // print_r wrapper
    pr(curl_getinfo($ch));
    var_dump($response);
    curl_close($ch);

Shell Exec代码 这段代码可以很好地实现我们想要的API,唯一的问题是我们没有得到响应体。

    $command = Configure::read('DM.CurlBinary') . ' -sb -k -v -X POST ';
    $command .= $url . ' -F "' . $data['file']['name'] . '=@' . $data['file']['location'] . '"';
    $command .= ' -H "AUTHORIZATION: Bearer ' . $authInfo->access_token . '"';
    $command .= ' -H "ACCEPT: application/json" -H "EXPECT: 100-continue"';
    $output = shell_exec($command);

    pr($output); // print_r wrapper
    pr($command);

实际CURL命令

curl -sb -k -v -X POST https://idolcamp.idol.io/api/v1/tracks/857707/records -F "record[new_file]=@/Users/someuser/Documents/Development/DittoCake/app/tmp/Idol_857707.flac" -H "AUTHORIZATION: Bearer speacialtokengoeshere" -H "ACCEPT: application/json" -H "EXPECT: 100-continue"

响应 这显示为422错误,因为我上传的文件已经存在,我会通过其ID检查它,但我们没有响应以获取ID并检查下次是否存在。

Adding handle: conn: 0x7f8e3c004000
Adding handle: send: 0
Adding handle: recv: 0
Curl_addHandleToPipeline: length: 1
- Conn 0 (0x7f8e3c004000) send_pipe: 1, recv_pipe: 0
About to connect() to idolcamp.idol.io port 443 (#0)
  Trying 212.83.129.122...
Connected to idolcamp.idol.io (212.83.129.122) port 443 (#0)
TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
Server certificate: *.idol.io
Server certificate: Gandi Standard SSL CA
Server certificate: UTN-USERFirst-Hardware
POST /api/v1/tracks/857707/records HTTP/1.1
User-Agent: curl/7.30.0
Host: idolcamp.idol.io
AUTHORIZATION: Bearer somespecialtoken
ACCEPT: application/json
EXPECT: 100-continue
Content-Length: 29600116
Content-Type: multipart/form-data; boundary=----------------------------3bd64d03cd9b

HTTP/1.1 100 Continue
} [data not shown]
HTTP/1.1 422 Unprocessable Entity
Server nginx is not blacklisted
Server: nginx
Date: Tue, 25 Aug 2015 15:08:00 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
X-Request-Id: 3803269a-18ea-4558-8623-08810a058030
X-Runtime: 1.112917
HTTP error before end of send, stop sending

Closing connection 0

API文档:请求

enter image description here

API文档:回复

enter image description here

0 个答案:

没有答案