2013-08-18 22 views
0

我使用imgur API上传图像,然后我想回显图像的链接。 下面是代码:回声部分的json代码

<?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); 

?> 

$json_returned是这样的:

{ 
    "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 
} 

如何我只能随声附和图片的网址?

+0

'回声json_decode($ json_returned,真) ['data'] ['link']' –

+0

@Dave Chen 我试过使用echo json_decode($ json_returned,true)['data'] ['link'],但输出结果是一个空白页面。可以帮我吗? – GigaHertz

+0

对不起,我是用flash写的。您需要先解码,然后读取数组索引。例如'$ json_returned = json_decode($ json_returned,true); echo $ json_returned ['data'] ['link'];'。 –

回答

0

Imgur要求通过HTTP访问API。

检出this answer关于如何使用认证文件。

您还可以禁用通过选项检查(不推荐!):

CURLOPT_SSL_VERIFYPEER => 0, 
CURLOPT_SSL_VERIFYHOST => 0 

然后才能访问该链接,只需使用:

$json_returned = json_decode($json_returned, true); 
echo $json_returned['data']['link'];