Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php获取Instagram媒体?_Php_Wordpress_Instagram Api - Fatal编程技术网

使用php获取Instagram媒体?

使用php获取Instagram媒体?,php,wordpress,instagram-api,Php,Wordpress,Instagram Api,我正在尝试从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)

我正在尝试从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;}

如果您有任何建议,我将不胜感激。

如果您知道用户名,我有另一种获取用户图像的方法 下面是我的github repo获取用户图像的示例 这是一份回购协议

下面的php代码用于获取用户图像 我正在使用卷曲和刮削图像 不使用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

建议检查您从您的定义中得到的错误,因为它似乎不起作用。会发生什么?你有错误吗?输出是什么?完成时没有任何错误…对不起,Kirk,没有错误,问题是图像URL没有出现。我觉得这可能与json_解码有关,因为如果我回显$result,就会得到原始数据读数。
<? 
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; ?>