为什么cURL exec也返回我的POST请求的头数据?

时间:2013-08-03 04:48:30

标签: php curl

我只是测试一些代码进行图像压缩,然后上传到Imgur API。但不仅仅是获取响应内容我似乎也得到了标题数据,我无法弄清楚如何解析JSON。这是我到目前为止所做的:

compress_test.php

include("imgur_upload.php");
echo compress_and_upload();

imgur_upload.php

function compress_and_upload(){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // for localhost
    curl_setopt($ch, CURLOPT_HEADER, FALSE); // tried this but no change
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . IMGUR_CLIENT_ID));
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => 'http://userserve-ak.last.fm/serve/300x300/51654499.png', 'type' => 'url'));

    $reply = curl_exec($ch);
    curl_close($ch);
    return $reply;
}

我期待回应的只是我相信的JSON数据。

这是我得到的:

BHTTP/1.1 200 OK
Server: nginx
Date: Sat, 03 Aug 2013 05:14:53 GMT
Content-Type: application/json
Content-Length: 325
Connection: keep-alive
Set-Cookie: IMGURSESSION=dhjtli4c84koo2jbr8lild7ji7; path=/; domain=.imgur.com
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Set-Cookie: _nc=1; path=/; domain=.imgur.com; httponly
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: Authorization, Content-Type, Accept, X-Mashape-Authorization
X-RateLimit-ClientLimit: 12500
X-RateLimit-ClientRemaining: 12473
X-RateLimit-UserLimit: 500
X-RateLimit-UserRemaining: 497
X-RateLimit-UserReset: 1375510414
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Set-Cookie: UPSERVERID=i-614a2006; path=/
Accept-Ranges: bytes
X-Imgur-Cached: 0

{"data":{"id":"HE981gx","title":null,"description":null,"datetime":1375506893,"type":"image\/png","animated":false,"width":300,"height":300,"size":458117,"views":0,"bandwidth":0,"favorite":false,"nsfw":null,"section":null,"deletehash":"qkl9lNDWCRR52Z0","link":"http:\/\/i.imgur.com\/HE981gx.png"},"success":true,"status":200}°

1 个答案:

答案 0 :(得分:-2)

我在自己的服务器上运行代码并使用了您的代码,但没有这个问题。