2012-04-18 5 views
0

저는 제가하고있는 일을 구현하는 방법에 대한 논리적 인 장애물에 있습니다. 저는 Instagram API (이 질문에 중요하지 않음)로 작업 중이며 JSON 응답에서 20 개의 결과와 다음 20 개의 결과에 대한 링크를 제공합니다. 초기 API 호출에서 응답을 얻을 수있는 OOP 라이브러리를 구축하려고합니다. next_url 값이 있는지 검색하고, 다음 20 개 응답을 얻고 더 이상 다음 URL이 없을 때까지 un를 만듭니다. . 꽤 JSON의 루핑 로직

현재 여기 처음 20 ...

function __apiCall($url, $post_parameters = FALSE) { 

     $curl_session = curl_init(); 

     curl_setopt($curl_session, CURLOPT_URL, $url); 

     if($post_parameters !== FALSE) { 
      curl_setopt ($curl_session, CURLOPT_POSTFIELDS, $post_parameters); 
     } 

     // Return the curl results to a variable 
     curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1); 

     curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, $this->codeigniter_instance->config->item('instagram_ssl_verify')); 

     $contents = curl_exec ($curl_session); 

     curl_close ($curl_session); 

     $return = json_decode($contents); 
     return $return; 


    } 

을 얻을 수있는 코드 그리고 여기에 JSON 응답에서 조각입니다 ...이 작업을 수행하는 방법에 대한 논리를 수집 할 수 없습니다 .. ..

stdClass Object 
(
    [pagination] => stdClass Object 
     (
      [next_url] => https://api.instagram.com/v1/locations/3937885/media/recent?min_timestamp=&min_id=&max_timestamp=&access_token=xxxxxxxxxxxx49414762bea69258210d8872&max_id=133226850290424667_21341717 
      [next_max_id] => 133226850290424667_21341717 
     ) 

    [meta] => stdClass Object 
     (
      [code] => 200 
     ) 

    [data] => Array 
     (
      [0] => stdClass Object 
       (
        [tags] => Array 
         (
         ) 

        [type] => image 
        [location] => stdClass Object 
         (
          [latitude] => 39.95022 
          [name] => Neiman Group 
          [longitude] => -75.168322 
          [id] => 3937885 
         ) 

        [comments] => stdClass Object 
         (
          [count] => 0 
          [data] => Array 
           (
           ) 

         ) 
+0

"OOP 라이브러리를 만들려고합니다."OOP는 어디에 있습니까? :) – PeeHaa

+0

글쎄, 여기에 붙여 넣기는 쓸모가 없다. 실제로는 기존의 라이브러리에서 작업하고있다. –

+0

'post_parameters'에는 무엇이 들어 있는가? 추가 전화시에도 필요합니까? – PeeHaa

답변

2
function getResults($url) 
{ 
    $gotAllResults = false; 
    $results = array(); 

    while(!$gotAllResults) { 
     $result = $this->__apiCall($url); 
     $results[] = $result; 

     if (!property_exist($result->pagination, 'next_url') { 
      $gotAllResults = true; 
     } else { 
      $url = $result->pagination->next_url; 
     } 
    } 

    return $results; 
} 

function __apiCall($url) 
{ 
     // settings for cURL 

     $contents = curl_exec($curl_session); 

     curl_close ($curl_session); 

     $result = json_decode($contents); 
     return $return; 
} 

BTW. 왜 __apiCall 함수 이름에 밑줄 2 개를 사용하고 있습니까? 그게 내게 무언가 magical이 일어나고 있다고 생각하게 만듭니다.

+0

끝내주게, 고마워, 내가 고쳤던 약간의 오타가 있었다. 그러나 당신 남자. .. 나는 나의 결승으로 대답 할 것이다. .. –

+0

@GThompson 나는 오탈자에 대해 도울 수 있었고, 유감스럽게 생각할 수 있었다. 그것이 단지 작동한다면 많은 재미는 없을 것이다 ;-) – PeeHaa

0
function __apiCall($url, $post_parameters = FALSE) { 

     // Initialize the cURL session 
     $curl_session = curl_init(); 

     // Set the URL of api call 
     curl_setopt($curl_session, CURLOPT_URL, $url); 

     // If there are post fields add them to the call 
     if($post_parameters !== FALSE) { 
      curl_setopt ($curl_session, CURLOPT_POSTFIELDS, $post_parameters); 
     } 

     // Return the curl results to a variable 
     curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1); 

     // There was issues with some servers not being able to retrieve the data through https 
     // The config variable is set to TRUE by default. If you have this problem set the config variable to FALSE 
     // See https://github.com/ianckc/CodeIgniter-Instagram-Library/issues/5 for a discussion on this 
     curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, $this->codeigniter_instance->config->item('instagram_ssl_verify')); 

     // Execute the cURL session 
     $contents = curl_exec ($curl_session); 

     // Close cURL session 
     curl_close ($curl_session); 

     // Return the response 
     return json_decode($contents); 


    } 

    function getResults($url){ 

     $gotAllResults = false; 
     $results = array(); 

     while(!$gotAllResults) { 
     $result = $this->__apiCall($url); 
     $results[] = $result; 

     if (!property_exists($result->pagination, 'next_url')) { 
      $gotAllResults = true; 
     } else { 
      $url = $result->pagination->next_url; 
     } 
    } 

    return $results; 

    }