json - Getting image data using the Imgur API and PHP -
i'm quite new php , i'm trying use imgur api (my code based on several tutorials) i'm trying image imgur display on web page. i'm using code
<?php $client_id = '<id>'; $ch = curl_init(); curl_setopt($ch,curlopt_url,'https://api.imgur.com/3/image/rnxusia'); curl_setopt($ch,curlopt_connecttimeout, 5); curl_setopt($ch,curlopt_followlocation, true); curl_setopt($ch,curlopt_returntransfer, true); curl_setopt($ch, curlopt_httpheader, array('authorization: client-id ' . $client_id)); $result = curl_exec($ch); curl_close($ch); $json = json_decode($result, true); ?> after converting $result associative array, i'm trying access data
$data = $json->data->link;
but there's nothing in $data , i'm getting trying property of non-object error. i'm assuming imgur didn't return data. doing wrong?
i read more curl , re-wrote code.
<?php $client_id = "<id>"; $c_url = curl_init(); curl_setopt($c_url, curlopt_ssl_verifypeer, false); curl_setopt($c_url, curlopt_returntransfer, true); curl_setopt($c_url, curlopt_url,"https://api.imgur.com/3/image/rnxusia"); curl_setopt($c_url, curlopt_httpheader, array('authorization: client-id ' . $client_id)); $result=curl_exec($c_url); curl_close($c_url); $json_array = json_decode($result, true); var_dump($json_array); ?> this wokred , var_dump($json_array); displayed array content.
$json_array['data']['link']; gives direct link image.
for album, changed url https://api.imgur.com/3/album/y1dzj , used loop image links
$image_array = $json_array["data"]["images"]; foreach ($image_array $key => $value) { echo $value["link"]; } i hope helps who's new imgur api.
Comments
Post a Comment