我只是測試一些圖像壓縮代碼,然後上傳到Imgur API。但是,我並沒有獲得響應內容,而是似乎也獲得了標題數據,而我無法弄清楚如何解析JSON。這是我到目前爲止有:爲什麼cURL exec還會返回我POST請求的標題數據?
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}°
你確定'CURLOPT_HEADER'沒有設置在某個地方嗎?如果將它設置爲「假」呢? – Brad
@Brad Yup,我試着將'CURLOPT_HEADER'設置爲false,但我仍然得到整件事情,所以無法解析JSON。我可以找到'{'的第一個索引,但我不應該這麼做。 – rtheunissen
@ paranoid-android什麼版本的PHP,什麼操作系統,你是用CLI還是類似mod_php? – SheetJS