2017-12-14 5 views
1

내 사이트 중 하나의 메인 페이지에서 가장 최근의 3 개의 그림을 Instagram 피드로 표시합니다. 최근특정 해시 태그로 Instagram 사진 가져 오기

function rudr_instagram_api_curl_connect($api_url){ 
    $connection_c = curl_init(); // initializing 
    curl_setopt($connection_c, CURLOPT_URL, $api_url); // API URL to connect 
    curl_setopt($connection_c, CURLOPT_RETURNTRANSFER, 1); // return the result, do not print 
    curl_setopt($connection_c, CURLOPT_TIMEOUT, 20); 
    $json_return = curl_exec($connection_c); // connect and get json data 
    curl_close($connection_c); // close connection 
    return json_decode($json_return); // decode and return 
} 
$access_token = 'access_token'; 
$image_return = 3; 
$user_search = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/users/self/media/recent?access_token=".$access_token); 

$user_id = $user_search->data[0]->id; // or use string 'self' to get your own media 
$return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/users/self/media/recent?access_token=".$access_token."&count=".$image_return); 

// var_dump($return); // if you want to display everything the function returns 

foreach ($return->data as $post) { 
    echo '<div class="col-sm-12 col-md-4 mb-3 text-center"> 
      <a href="'.$post->link.'" target="_blank"> 
      <img src="' . $post->images->standard_resolution->url . '" class="img-fluid img-thumbnail bx-shadow"/> 
      </a> 
      <p><i class="fa fa-tag" aria-hidden="true"></i> ' . $post->caption->text . '<br><i class="fa fa-heart" aria-hidden="true"></i> '.$post->likes->count.' <i class="fa fa-comment" aria-hidden="true"></i> '.$post->comments->count.'<br><i class="fa fa-clock-o" aria-hidden="true"></i> ' . date('M j, Y', $post->created_time) . '</p> 
      </div>'; 
} 

내가 난 단지 검색 할 수있는 방법을 찾을 거라고 생각 그래서 내가 반드시 사이트에 표시하지 않도록 인스 타 그램에 사진을 업로드했습니다 : 나는 다음과 같은 코드로이 달성 한 특정 해시 태그가있는 이미지 빠른 Google 검색에서 여러 솔루션이 나타 났지만 하나는 기존 코드와 유사했습니다. 이 솔루션은 여기에서 찾을 수 있습니다 : Fetch All Photos With A Hashtag

나는 내가 $hashtag = "bodypiercing";의 변수를 추가하고 $return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/tags/".$hashtag."https://stackoverflow.com/users/self/media/recent?access_token=".$access_token."&count=".$image_return);로 URL에 추가하여 내 코드를 수정 단순히 URL에 tags 변수가 있었다 용액에 나타났습니다.

function rudr_instagram_api_curl_connect($api_url){ 
    $connection_c = curl_init(); // initializing 
    curl_setopt($connection_c, CURLOPT_URL, $api_url); // API URL to connect 
    curl_setopt($connection_c, CURLOPT_RETURNTRANSFER, 1); // return the result, do not print 
    curl_setopt($connection_c, CURLOPT_TIMEOUT, 20); 
    $json_return = curl_exec($connection_c); // connect and get json data 
    curl_close($connection_c); // close connection 
    return json_decode($json_return); // decode and return 
} 
$access_token = 'access_token'; 
$image_return = 3; 
$hashtag = "bodypiercing"; 
$user_search = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/users/self/media/recent?access_token=".$access_token); 

$user_id = $user_search->data[0]->id; // or use string 'self' to get your own media 
$return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/tags/".$hashtag."https://stackoverflow.com/users/self/media/recent?access_token=".$access_token."&count=".$image_return); 

// var_dump($return); // if you want to display everything the function returns 

foreach ($return->data as $post) { 
    echo '<div class="col-sm-12 col-md-4 mb-3 text-center"> 
      <a href="'.$post->link.'" target="_blank"> 
      <img src="' . $post->images->standard_resolution->url . '" class="img-fluid img-thumbnail bx-shadow"/> 
      </a> 
      <p><i class="fa fa-tag" aria-hidden="true"></i> ' . $post->caption->text . '<br><i class="fa fa-heart" aria-hidden="true"></i> '.$post->likes->count.' <i class="fa fa-comment" aria-hidden="true"></i> '.$post->comments->count.'<br><i class="fa fa-clock-o" aria-hidden="true"></i> ' . date('M j, Y', $post->created_time) . '</p> 
      </div>'; 
} 

그러나이 결과는 다음과 : 여기

는 전체 코드입니다

Notice: Trying to get property of non-object in /home/jesse666toxik/public_html/php/index.main.php on line 42

이 하나 개의 변수가 할 수 있도록 내가 잘못했을 수도 무엇

Warning: Invalid argument supplied for foreach() in /home/jesse666toxik/public_html/php/index.main.php on line 42

이? var_dump

결과 :

Notice 
: Undefined property: stdClass::$data in 
/home/jesse666toxik/public_html/php/index.main.php 
on line 
37 


Notice 
: Trying to get property of non-object in 
/home/jesse666toxik/public_html/php/index.main.php 
on line 
37 

NULL 
Notice 
: Trying to get property of non-object in 
/home/jesse666toxik/public_html/php/index.main.php 
on line 
42 


Warning 
: Invalid argument supplied for foreach() in 
/home/jesse666toxik/public_html/php/index.main.php 
on line 
42 
+0

'var_dump ($ return)'의 주석 처리를 제거하십시오. 반환되는 것은 무엇입니까? 여러분의 코드는 배열을 포함하는'data' 매개 변수를 가진 객체가 될 것으로 기대합니다. 그럴까요? – ceejayoz

+0

'$ return' 변수가 사용자를 반환하는 것처럼 보입니다. api 호출은'$ user_search'에 사용 된 것과 같습니다. –

+0

@ceejayoz'var_dump' 결과를 편집하고 포함했습니다. @MacroMan은 원래 튜토리얼의 예제였습니다. 왜 그런지는 모르겠지만 효과가 있습니다. –

답변

0

잘못된 URL을 사용하면 링크 한 페이지의 정보에 의해 판단, 당신은 원래 사용자의 URL에 해시 태그 URL의 일부를 삽입했습니다.

$url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id; 

는 언제나 대답은 API documentation에서 찾을 수 있습니다

인용 문서에서는이 URL을 사용합니다. 특정 사용자의 해시 태그로 검색 할 수 없습니다.

+0

글쎄, 내 자신의 이미지를 검색하고 있기 때문에 내 URL에'users/self'를 사용하고 있습니다. 내 URL에'/ tags/hashtag'을 삽입하는 것만으로도 여전히 수익을 제한하는 것으로 보입니다. 나는 약간의 일을하고 그 URL을 시도 할 것이다. –

+0

슬프게도 그 URL은 내가 가지고있는 access_token을 요구하고 있습니다. 추가되면 이미지를 반환하지 않습니다. URL을 코드가 아닌 브라우저에 직접 넣었습니다. 이것은'{ "meta": { "code": 400, "error_type": "OAuthPermissionsException", "error_message": "이 요청은 scope = public_content이지만이 액세스 토큰은이 범위로 승인되지 않습니다. 사용자는 scope = public_content를 사용하여이 권한을 부여 받아 응용 프로그램을 다시 인증해야합니다. "}} ' –

+0

API의 태그 요청에 대한 끝 점이 3 개뿐입니다. 태그 객체에 대한 정보를 얻습니다. 최근에 태그가 지정된 미디어의 목록을 가져오고 이름으로 태그를 검색하십시오. 사용자가 제한 할 수있는 옵션이 없습니다.가장 좋은 방법은 해시 태그를 사용하여 웹 사이트에서 원하는 이미지에 사용할 다른 사람이 아닌 다음 검색을 수행하는 것입니다. – miknik

관련 문제