1

I'm just testing some code for image compression and then uploading to the Imgur API. But instead of just getting the response content I seem to be getting the header data as well and I can't figure out how to just parse the JSON. Here's what I have so far:

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;
}

What I'm expecting to be echoed is just the JSON data I believe.

Here is what I'm getting:

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}°
4

1 に答える 1