从Instagram API获取结果以在Wordpress中使用


Get results from Instagram API to use in Wordpress

我一直在阅读客户(应用程序(在Instagram上的基本身份验证。 从我在阅读 cURL 时收集到的信息来看,大多数线程都在说最好的选择。 我还在Gihub上找到了一个Instagram PHP API包装器,但我认为我不需要走这条路来做我需要的事情。

我想做的是这样的:

一个函数检查当前用户元中的"instagram_user"(Instagram用户ID(和"instagram_token"(访问令牌(。

如果未设置 usermeta,这意味着他们尚未授权客户端,则会显示指向授权 URL 的链接。 他们点击链接,授权应用程序并返回我们的网站。 我需要知道如何在他们返回后检索这些数据,以便我可以使用add_user_meta将他们的令牌和 ID 保存到user_meta表中。

//Get current user
$user_id = get_current_user_id();
$instagram_userID = get_user_meta($user_id, 'instagram_user', true);
$access_token = get_user_meta($user_id, 'instagram_token', true);
if(!$access_token || !$instagram_userID){
    echo '<p style="text-align:center;">You need to authorize Instagram in order to see your feed here.</p><p><a class="btn btn-success" href="https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID-HERE&redirect_uri=http://localhost:8888&response_type=code" title="Click here to verify" style="display:block;margin:0 auto;">Verify Now</a>';
} else { //User is authorized ?>
<div id="user-instagram-feed"></div>
    <script type="text/javascript">
    jQuery(document).ready(function($)){
        var access_token = "ACCESS_TOKEN";   
        var user_id = "USER_ID";
        var url = "https://api.instagram.com/v1/users/"+user_id+"?access_token="+access_token+"&callback=?";
        $.getJSON(url, function(data) {
            $("#user-instagram-feed").append("<img src='"+data.data.profile_picture+"' />");
        });
    });
      </script>
<?php }

陷入困境的地方是,一旦用户授权应用程序并返回我们的网站,我如何访问数据对象以提取令牌和用户 ID?

旁注:这应该不会有什么区别,但用户提要将位于 buddypress 安装的小部件中,因此它并不总是我们尝试显示的相同用户提要,这就是为什么我们选择将其保存到数据库并运行条件以查看它是否存在于用户。

这是否意味着您没有获得instagram_userinstagram_token价值?对不起,我的英语不是那么好。

这实际上需要像使用Instagram登录这样的东西。你试过Instagram PHP API库吗?假设您的回调页面与您编写的代码相同(不过,您应该创建自定义回调页面(。所以,基本上是这样的。

注意:未测试。我从不使用这个库。

require '../src/Instagram.php';
use MetzWeb'Instagram'Instagram;
if( !session_id() )session_start(); //check whether session isn't started
$instagram = new Instagram(array(
    'apiKey'      => 'YOUR_APP_KEY',
    'apiSecret'   => 'YOUR_APP_SECRET',
    'apiCallback' => 'YOUR_APP_CALLBACK' //this code's URL
));
$user_id = get_current_user_id();
//Once user returned, get the token then store it (as you wish)
if (isset($_GET['code']){
$data = $instagram->getOAuthToken($code);
$token = $data->access_token;
add_user_meta( $user_id, 'instagram_token', $token);
}    
//Get current user
$access_token = get_user_meta($user_id, 'instagram_token', true);
$instagram->setAccessToken($access_token);
$user_ig = $instagram->getUser();
$profile_picture = $user_ig->data->profile_picture;
if(!$access_token || !$instagram_userID){
    echo '<p style="text-align:center;">You need to authorize Instagram in order to see your feed here.</p><p><a class="btn btn-success" href="'.$instagram->getLoginUrl().'" title="Click here to verify" style="display:block;margin:0 auto;">Verify Now</a>';
} else { //User is authorized ?>
<div id="user-instagram-feed"></div>
    <script type="text/javascript">
    jQuery(document).ready(function($)){
            $("#user-instagram-feed").append("<img src='<?php echo $profile_picture; ?>' />");
    });
    </script>
<?php }

多亏了@Chay22我才能让它工作。 我正在为可能正在寻找相同内容的其他人发布下面的代码。

//Get current user & data
            $user_id = get_current_user_id();
            $instagram_userID = get_user_meta($user_id, 'instagram_user', true);
            $access_token = get_user_meta($user_id, 'instagram_token', true);
            $siteURL = site_url();
            //Instagram API calls
            if (isset($_GET['code'])){
                $code = $_GET['code'];
                //API params
                $apiData = array(
                  'client_id'       => '{CLIENT-ID-HERE}',
                  'client_secret'   => '{CLIENT-SECRET-HERE}',
                  'grant_type'      => 'authorization_code',
                  'redirect_uri'    => $siteURL,
                  'code'            => $code
                );
                $apiHost = 'https://api.instagram.com/oauth/access_token';
                //cURL API request
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $apiHost);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $jsonData = curl_exec($ch);
                curl_close($ch);
                $user = json_decode($jsonData); 
                // If we have a toke and ID set it up in WP
                if($user->access_token) {
                    $instagram = $user->user;
                    add_user_meta($user_id, 'instagram_token', $user->access_token, true);
                    add_user_meta($user_id, 'instagram_user', $instagram->id, true);
                    echo 'added meta';
                }
                //If error return error
                if($user->error_type && $user->error_message){
                    echo'<div class="alert alert-danger">Error: ' . $user->error_type . ' Error Message: ' . $user->error_message . '</div>';
                }
            }
            if(!$access_token || !$instagram_userID){
                //No token set yet
                echo '<p style="text-align:center;">You need to authorize Instagram in order to see your feed here.</p><p><a class="btn btn-success" href="https://api.instagram.com/oauth/authorize/?client_id={CLIENT-ID-HERE}&redirect_uri={REDIRECT-URI-HERE}&response_type=code" title="Click here to verify" style="display:block;margin:0 auto;">Verify Now</a></p>';
            } else { //User is authorized ?>
            <div id="user-instagram-feed"></div>
                <script type="text/javascript">
                jQuery(document).ready(function($) {
                    var access_token = "<?php echo $access_token; ?>";   
                    var user_id = "<?php echo $instagram_userID; ?>";
                    var url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token='+access_token;
                    $.ajax({
                        type:"GET",
                        dataType:"jsonp",
                        url: url,
                        success: function(data){
                            var images = data.data[0].images;
                            //alert(images.standard_resolution.url);
                            $("#user-instagram-feed").append('<a href="' + data.data[0].link + '" target="_blank"><img src="'+images.standard_resolution.url + '" /></a>');
                        },
                        error: function (data, xhr, desc, err)
                        {
                            $("#user-instagram-feed").append('<div class="alert alert-danger">An Error Occurred: ' + xhr + ' ' + desc + ' ' + err + '</div>');
                            console.log(data);
                        }
                    });
                });
                  </script>
            <?php }

干杯:)