1

I am trying to save Instagram images on my local machine through API and using the file_put_contents php method

I have tried sleep() method

ini_set('max_execution_time', 400);

$access_token = "";
$photo_count = 8;
$json_link = "https://api.instagram.com/v1/users/self/media/recent/?";
$json_link .="access_token={$access_token}&count={$photo_count}";

$json = file_get_contents($json_link);

$obj = json_decode(preg_replace('/("\w+"):(\d+)/', '\\1:"\\2"', $json), true);
$cnt = 1;

foreach ($obj['data'] as $post){
    $pic_text = $post['caption']['text'];
    $pic_link = $post['link'];
    $pic_like_count = $post['likes']['count'];
    $pic_comment_count=$post['comments']['count'];
    $pic_src=str_replace("http://", "https://", $post['images']['standard_resolution']['url']);
    $pic_created_time=date("F j, Y", $post['caption']['created_time']);
    $pic_created_time=date("F j, Y", strtotime($pic_created_time . " +1 days"));
    $img = $_SERVER['DOCUMENT_ROOT']."/assets/images/insta-images/".$cnt.".jpg";
    file_put_contents($img, file_get_contents($pic_src));
    $cnt++;
}

With the above code, there are only 4 images are successfully saved on my local machine but I have set photo count 8, the script is running for the only 1 minute.

I want a minimum of 8 images should save

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
Ganesh
  • 81
  • 1
  • 12

0 Answers0