在php中拉Instagram照片

时间:2015-09-25 05:58:55

标签: php instagram

我要把头发拉出来。任何人都可以帮助我让这个工作我确定它是一个愚蠢的东西..我已经得到了所有的PHP错误,但我无法让图像显示出来。代码如下......

    <!DOCTYPE html>
<html>
    <link rel="stylesheet" href="jquery.fancybox-1.3.4.css" type="text/css">
    <script type='text/javascript' src='jquery.min.js'></script>
    <script type='text/javascript' src='jquery.fancybox-1.3.4.pack.js'></script>
    <script type="text/javascript">
        $(function() {
            $("a.group").fancybox({
                'nextEffect'    :   'fade',
                'prevEffect'    :   'fade',
                'overlayOpacity' :  0.8,
                'overlayColor' : '#000000',
                'arrows' : false,
            });         
        });
    </script>

    <?php
        // Supply a user id and an access token
        $userid = "1d458ab0c149424c812e664c32b48149";
        $accessToken = "c195717e379f48c68df451cc3d60524a";

        // Gets our data
        function fetchData($url){
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, $url);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($ch, CURLOPT_TIMEOUT, 20);
             $result = curl_exec($ch);
             curl_close($ch); 
             return $result;
        }

        // Pulls and parses data.
        $result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
        $result = json_decode($result);
    ?>


    <?php if(!empty($result->data)): ?>
    <?php foreach ($result->data as $post){ ?>
    <!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
    <a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
    <?php } ?>
    <?php endif ?>
</html>

1 个答案:

答案 0 :(得分:0)

您需要的是在各个点添加一些检查,以找出从Instagram回来的内容并处理任何问题。在调试时,在所有地方坚持var_dump()可以帮助您了解问题所在。

以下是PHP部分的示例,其中包含一些额外的检查:

<?php
    // Supply a user id and an access token
    $userid = "USER ID";
    $accessToken = "ACCESS TOKEN";

    // Gets our data
    function fetchData($url){
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_TIMEOUT, 20);

         $result = curl_exec($ch);

         $info = curl_getinfo($ch);

         // Check a response was returned
         if ($info['http_code'] == '404') {
             echo ('Error: HTTP 404 returned, bad request');
             die();
         }
         curl_close($ch); 

         return $result;
    }

    // Pulls and parses data.
    $result = fetchData("https://api.instagram.com/v1/users/{$userid}/?access_token={$accessToken}");
    $result = json_decode($result);

    // Check the json_decode succeeded
    if (empty($result)) {
        echo "Error: JSON not returned from API";
        die();
    }

    // Check no error was returned from Instagram
    if ($result->meta->code != 200) {
        echo "Error: ".$result->meta->error_message;
        die();
    }
?>

如果您计划使用Instagram API进行大量工作,您可能需要查看一个库来完成大部分繁重工作。 This one似乎是目前最受欢迎的。