<?php
error_reporting(0);
header('Content-Type: application/json;');
$test = $_GET['test'] == 1;

$get_url = $_GET['url'];
if ($get_url == null) {
    $print_false_xd = array();
    $print_false_xd['res']['text']= "لطفا لینک پست خودرا بگذارید مانند نمونه instadw.php?url=https://www.instagram.com/p/CroXqJMI104/?igshid=YmMyMTA2M2Y=";
    echo json_encode(['ok' => -1, 'Results' =>$print_false_xd, 'api' => 'hajiapi'], 448);
    die;
}

try {
    $context = stream_context_create(['http'=>[ 'timeout' => 20]]);
    $params = ['key' => 'UQUtiEQiMqfpDxv105jlwhyoX5apvmmy', 'url'=>"$get_url"];
        
    if(strpos($get_url, '/reel/') || strpos($get_url, '/p/')) // post or reels
    {
        $endpoint = "https://api3.haji-api.ir/sub/insta/post";
        $url = $endpoint . '?' . http_build_query($params);
        
        if($test) {
             var_dump($get_url);
             var_dump($url);
             die;
        }
        
        $end_url = file_get_contents($url, false, $context);
        if($end_url === false)
            throw new Exception("Cannot access '$url' to read contents.");
            
        if($test) {
             //var_dump($end_url);
             //die;
        }

        $serviceResultArray = json_decode($end_url,true);
    
        if(!$serviceResultArray['success'] || !isset($serviceResultArray['result']['medias'][0]))
            throw new Exception("result: " . $end_url); 
        
        $resultnext = [
            'number'=> count($serviceResultArray['result']['medias']) -1
        ];
        
        $index = 0;
        foreach ($serviceResultArray['result']['medias'] as $storyItem)
            $resultnext[$index++] = [
                'link' => $storyItem, 
                'caption' => $serviceResultArray['result']['caption']
            ];
        
        if($test) {
             var_dump($resultnext);
             die;
        }

        echo json_encode(['ok' => 1, 'Results' =>$resultnext, 'api' => 'hajiapi'], 448);
    }
    elseif(strpos($get_url, '/stories/'))
    {
        $endpoint = "https://api3.haji-api.ir/sub/insta/story";
        $url = $endpoint . '?' . http_build_query($params);
        
        if($test) {
             var_dump($get_url);
             var_dump($url);
             die;
        }
        
        $end_url = file_get_contents($url, false, $context);
        if($end_url === false)
            throw new Exception("Cannot access '$url' to read contents.");
            
        if($test) {
             //var_dump($end_url);
             //die;
        }

        $serviceResultArray = json_decode($end_url,true);
    
        if(!$serviceResultArray['success'] || !isset($serviceResultArray['result']['media']))
            throw new Exception("result: " . $end_url); 
        
        $resultnext = [
                'number'=> 0,
                0 =>[
                    'link' => $serviceResultArray['result']['media'],
                    'caption' => $serviceResultArray['result']['caption']
                ]
            ];
        
        if($test) {
             var_dump($resultnext);
             die;
        }

        echo json_encode(['ok' => 1, 'Results' =>$resultnext, 'api' => 'hajiapi'], 448);
    }
    elseif(strpos($get_url, '/s/'))  //highlight
    {
        $endpoint = "https://api3.haji-api.ir/sub/insta/highlight";
        $url = $endpoint . '?' . http_build_query($params);
        
        $end_url = file_get_contents($url, false, $context);
        if($end_url === false)
            throw new Exception("Cannot access '$url' to read contents.");
            
        if($test) {
             //var_dump($end_url);
             //die;
        }
        $serviceResultArray = json_decode($end_url,true);
    
        if(!$serviceResultArray['success'] || !isset($serviceResultArray['result']['media']))
            throw new Exception("result: " . $end_url); 
        
        $resultnext = [
                'number'=> 0,
                0 =>[
                    'link' => $serviceResultArray['result']['media'],
                    'caption' => ''
                ]
            ];
        
        if($test) {
             var_dump($resultnext);
             die;
        }

        echo json_encode(['ok' => 1, 'Results' =>$resultnext, 'api' => 'hajiapi'], 448);
    }
    
    else
        echo json_encode(['ok' => -1, 'Results' =>$print_false_xd, 'api' => 'hajiapi'], 448);
}
catch (Exception $ex) {
    $print_false_xd = [];
    $print_false_xd['res']['text'] = "code:" . $ex->getCode() . " - Message:" . $ex->getMessage();
    echo json_encode(['ok' => 0, 'Results' =>$print_false_xd, 'api' => 'hajiapi'], 448);
    exit;
}
