0

I'm using imgur API to upload an image and then I'd like to echo the link of the image. Here is the code:

<?php
$client_id = 'xxxxxxxxxxx';

$file = file_get_contents("http://mywebsite.com/image.jpeg");

$url     = 'https://api.imgur.com/3/image.json';
$headers = array(
    "Authorization: Client-ID $client_id"
);
$pvars   = array(
    'image' => base64_encode($file)
);

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_POST => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => $pvars
));

$json_returned = curl_exec($curl); // blank response


echo $json_returned;
curl_close($curl);

?>

The $json_returned is something like this:

{
    "data": {
        "id": "93MubeE",
        "title": null,
        "description": null,
        "datetime": 1376842908,
        "type": "image/jpeg",
        "animated": false,
        "width": 2197,
        "height": 1463,
        "size": 70884,
        "views": 0,
        "bandwidth": 0,
        "favorite": false,
        "nsfw": null,
        "section": null,
        "deletehash": "bk5k8HrAeH8aOtW",
        "link": "http://i.imgur.com/93MubeE.jpg"
    },
    "success": true,
    "status": 200
}

How can I echo image's url only?

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
GigaHertz
  • 15
  • 4
  • `echo json_decode($json_returned, true)['data']['link']` – Dave Chen Aug 18 '13 at 16:24
  • @Dave Chen I tried using echo json_decode($json_returned, true)['data']['link'] but the output was a blank page..Can you help me? – GigaHertz Aug 18 '13 at 16:30
  • Sorry, I wrote it in a flash. You need to decode it first, then read array index. Such as `$json_returned = json_decode($json_returned, true); echo $json_returned['data']['link'];`. – Dave Chen Aug 18 '13 at 16:32
  • @DaveChen the output is still a blank page. Here is the whole code http://pastebin.com/RKB5cct7. Feel free to test it and find the errors.Thanks! – GigaHertz Aug 18 '13 at 16:48

1 Answers1

0

Imgur requires the API to be accessed through HTTPs.

Check out this answer on how to use certification files.

You may also disable the check through options (not recommended!):

CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0

Then, to access the link, simply use:

$json_returned = json_decode($json_returned, true);
echo $json_returned['data']['link'];
Community
  • 1
  • 1
Dave Chen
  • 10,887
  • 8
  • 39
  • 67