2015-01-29 3 views
0

Instagram API를 사용하려고 할 때 이미지와 해당 URL을 화면에 표시하도록 관리했습니다. 그러나 내가 이미지와 관련된 다양한 메타 데이터에 액세스하는 방법을 볼 수 있도록 JSON 객체를 화면에 인쇄하는 방법이 있는지 궁금합니다. 사용자, 댓글 등등. 그래서 $ item-> url과 같은 for 루프를 통해 액세스 할 수 있습니다.Instagram API를 사용할 때 JSON 객체 인쇄

print_r 및 var_dump를 사용하려고했지만 모두 구문 분석 오류가 발생했습니다.

감사합니다.

<?php 

    /** 
    * denotes that the return data is formated as JSON rather 
    * than plain text, necessary for the js to read the data properly 
    **/ 
    header('Content-type: application/json'); 

    $client = "ebcfbebe699c4c35869fbbf2a63d92a6"; 

    $query = $_POST['q']; 

    $api = "https://api.instagram.com/v1/tags/".$query."/media/recent?client_id=".$client; 

    /** 
    * Pulling data from the API 
    * 
    **/ 

    function get_curl($url) { 

     if (function_exists('curl_init')) { 

      // first initialise 

      $ch = curl_init(); 

      // set the options including the URL 

      curl_setopt($ch, CURLOPT_URL,$url); 

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

      curl_setopt($ch, CURLOPT_HEADER, 0); 

      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

      // next execute and fetch the result 

      $output = curl_exec($ch); 

      // error reporting 

      echo curl_error($ch); 

      // free up the cURL handle 

      curl_close($ch); 

      return $output; 
     } 

     // if cURL not installed, simply use file_get_contents to retrieve data from URL 

     else { 
      return file_get_contents($url); 
     } 
    } 

    // pull the data from the get_curl function and store in $response 

    $response = get_curl($api); 


    /** 
    * Oraganising and returning the data 
    * 
    */ 

    // create an array for the instagram images 
    $images = array(); 

    if ($response) { 

    // foreach loop to pull out JSON data objects one by one 
    // the values we need $src. $thumb and $url 
     foreach(json_decode($response)->data as $item) { 
      $src = $item->images->standard_resolution->url; 
      $thumb = $item->images->thumbnail->url; 
      $url = $item->link; 

      $images[] = array(
       "src" => htmlspecialchars($src), 
       "thumb" => htmlspecialchars($thumb), 
       "url" => htmlspecialchars($url), 

       ); 
     } 
    } 

    print_r(str_replace('\\/','/', json_encode($images))); 

    die(); 

?> 
+1

'var_dump'를 사용했을 때 rendred 오류가 무엇입니까 – smarber

답변

0

print_r (json_decode ($ response))해야합니다.

보고있는 오류의 출력은 무엇입니까?

+0

오류가 계속 발생하는 것 같습니다. 오류 : 구문 분석 오류 – pocockn

+0

json_last_error()는 무엇을 인쇄합니까? 오류 코드는 http://php.net/manual/en/function.json-last-error.php에서 찾을 수 있습니다. – Outspaced

관련 문제