1

我将这种结构用于 Instagram:

<?php
        // Supply a user id and an access token
        $userid = "--user--";
        $accessToken = "--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);
             curl_close($ch); 
             return $result;
        }

        // Pulls and parses data.
        $result = fetchData("https://api.instagram.com/v1/users/--user--/media/recent/?access_token=--token--");
        $result = json_decode($result);
    ?>


    <?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 endforeach ?>

但是这个打印:

警告:在第 40 行的 /home/mysite/index.php 中为 foreach() 提供了无效参数

40.行:

<?php foreach ($result->data as $post): ?>

这个结构有什么问题?

这个结构是http://www.blueprintinteractive.com/blog/how-instagram-api-fancybox-simplified

4

3 回答 3

1

好的,只是为了检查-在您的代码中,您有$userid = "--user--"and $accessToken = "--token--",然后在 URL 中,您有--user--并且--token--再次-您将它们替换为$useridand $accessToken?如果不是,问题可能只是 Instagram 正在返回一个错误,进而导致您的代码失败。

如果您考虑了上述情况但仍然一无所获,则$result->data在循环 ( var_dump($result->data)) 之前打印 的值并查看变量包含的内容。

如果上面没有显示任何内容,请尝试var_dump($result)- 可能json_decode()失败并将显示false

如果$result->data是一个数组,只是你没有数据(如果没有结果,我不确定 Instagram 会返回什么),在foreach()循环之前添加一个检查以查看数组是否为空。

<!-- This will output the contents of '$result->data' - let us know what it is -->
<?php var_dump($result->data)`) ?>

<?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 endforeach ?>
<?php endif ?>
于 2012-10-30T12:59:12.493 回答
1
<?php
        // Supply a user id and an access token
        $userid = "*****";
        $accessToken = "*********";

        // 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 endforeach ?>
<?php endif ?>

在它工作的在线编译器上尝试了这个。

于 2014-04-27T01:47:32.227 回答
1
<?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 endforeach ?>

这是错误的语法将其替换为:

<?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 } ?>
于 2015-06-03T16:11:27.907 回答