0

我正在尝试使用 instagram API,我已经设法将图像及其 url 放置在屏幕上。但是我想知道是否有一种方法可以将 JSON 对象打印到屏幕上,这样我就可以看到如何访问与图像相关的各种元数据。诸如用户、评论等之类的东西。所以我可以通过我的 for 循环来访问它们,比如 $item->url 。

我曾尝试使用 print_r 和 var_dump 但都产生了解析错误。

谢谢!

<?php

    /** 
    * denotes that the return data is formated as JSON rather 
    * than plain text, necessary for the js to read the data properly
    **/
    header('Content-type: application/json');

    $client = "ebcfbebe699c4c35869fbbf2a63d92a6";

    $query = $_POST['q'];

    $api = "https://api.instagram.com/v1/tags/".$query."/media/recent?client_id=".$client;

    /** 
    * Pulling data from the API
    * 
    **/

    function get_curl($url) {

        if (function_exists('curl_init')) {

            // first initialise

            $ch = curl_init();

            // set the options including the URL

            curl_setopt($ch, CURLOPT_URL,$url);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

            curl_setopt($ch, CURLOPT_HEADER, 0);

            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

            // next execute and fetch the result

            $output = curl_exec($ch);

            // error reporting

            echo curl_error($ch);

            // free up the cURL handle

            curl_close($ch);

            return $output;
        }

        // if cURL not installed, simply use file_get_contents to retrieve data from URL

        else {
            return file_get_contents($url);
        }
    }

    // pull the data from the get_curl function and store in $response

    $response = get_curl($api);


    /**
     * Oraganising and returning the data
     * 
    */

    // create an array for the instagram images
    $images = array();

    if ($response) {

    // foreach loop to pull out JSON data objects one by one
    // the values we need $src. $thumb and $url
        foreach(json_decode($response)->data as $item) {
            $src = $item->images->standard_resolution->url;
            $thumb = $item->images->thumbnail->url;
            $url = $item->link;

            $images[] = array(
                "src" => htmlspecialchars($src),
                "thumb" => htmlspecialchars($thumb),
                "url" => htmlspecialchars($url),

                );
        }
    }

    print_r(str_replace('\\/','/', json_encode($images)));

    die();

?>
4

1 回答 1

0

print_r(json_decode($response)) 应该这样做。

您看到的错误的输出是什么?

于 2015-01-29T15:43:18.247 回答