使用php获取Instagram媒体?

时间:2018-03-14 02:22:44

标签: php wordpress instagram-api

我正试图从Instagram用户那里获取图像(这是我们自己的帐户)。下面是我正在使用的代码,但它似乎不起作用。我确实有正确的userID和accessToken被输入代码。

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;
                    }

$result = fetchData("https://api.instagram.com/v1/users/".$userID."/media/recent/?access_token=".$accessToken);
$theinfo = json_decode($result, true);

foreach ($theinfo->data as $post){

echo $post->images->standard_resolution->url;}

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果您知道用户名
,我还有另一种获取用户图像的方法 这是我的github仓库的link以获取用户图像
这里是repo的link

以下php代码用于获取用户图像
我正在使用curl&刮图像 不使用instagram api

function scrape_insta_user_images($username) {
    $insta_source = file_get_contents('https://www.instagram.com/'.$username.'/'); // instagram user url
    $shards = explode('window._sharedData = ', $insta_source);
    $insta_json = explode(';</script>', $shards[1]); 
    $insta_array = json_decode($insta_json[0], TRUE);
    return $insta_array; // this return a lot things print it and see what else you need
}

$username = 'pakistan'; // user for which you want images 
$results_array = scrape_insta_user_images($username);
//echo '<pre>';
//print_r($results_array);
//echo '<pre>';
$limit = 56; // provide the limit thats important because one page only give some images.
$image_array= array(); // array to store images.
    for ($i=0; $i < $limit; $i++) {     
        //new code to get images from json  
        if(isset($results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i])){
            $latest_array = $results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i]['node'];
            $image_data  = '<img src="'.$latest_array['thumbnail_src'].'">'; // thumbnail and same sizes 
            //$image_data  = '<img src="'.$latest_array['display_src'].'">'; actual image and different sizes 
            array_push($image_array, $image_data);
        }
    }
    foreach ($image_array as $image) {
        echo $image;// this will echo the images wrap it in div or ul li what ever html structure 
    }
    // for getting all images have to loop function for more pages 
    // for confirmation  you are getting correct images view 
    //https://www.instagram.com/username

答案 1 :(得分:1)

<? 
function getPosts($username)
    {
        if (function_exists('curl_init')) {
            $url = "https://www.instagram.com/" . $username . "/?__a=1";
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 20);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $response = curl_exec($ch);
            curl_close($ch);
            return json_decode($response);
        } else {
            return false;
        }
    }
$arResult["POSTS"] = getPosts("instagram"); 
?>

<? foreach ($arResult["POSTS"]->graphql->user->edge_owner_to_timeline_media->edges as $key => $post): ?>
        <div class="col-md-2 col-sm-3 col-xs-4">
            <a href="https://www.instagram.com/p/<?=$post->code?>" target="_blank">
                <img src="<?=$post->node->thumbnail_resources[3]->src?>">
                <div class="instagram-info">
                    <div class="instagram-icon instagram-icon--likes">
                        <span><? echo number_format($post->node->edge_liked_by->count, 0, '.', ' '); ?></span>
                    </div>
                    <div class="instagram-icon instagram-icon--comments">
                        <span><? echo number_format($post->node->edge_media_to_comment->count, 0, '.', ' '); ?> <span class="horizontal-line"></span></span>
                    </div>
                </div>
            </a>
        </div><? endforeach; ?>