【问题标题】:Get Instagram media using php?使用 php 获取 Instagram 媒体?
【发布时间】:2023-04-01 03:27:01
【问题描述】:

我正在尝试从 Instagram 用户(这是我们自己的帐户)那里获取图像。下面是我正在使用的代码,但它似乎不起作用。我确实将正确的用户 ID 和 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;}

任何建议将不胜感激。

【问题讨论】:

  • 建议检查您从 curl 请求中得到的错误
  • 定义“似乎不起作用”。发生什么了?你有错误吗? php.net/curl_error 输出什么?
  • 完成没有任何错误...
  • 对不起柯克,没有错误,问题是图片网址没有出现。我觉得这可能与 json_decode 有关,因为如果我回显 $result 我会得到原始数据读数。

标签: php wordpress instagram-api


【解决方案1】:

如果你知道用户名,我还有另一种获取用户图像的方法
这是我的 github 存储库的 link 以获取用户图像
这是回购的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

【讨论】:

    【解决方案2】:
    <? 
    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; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多