<?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' => false, 'channel' => '@xdrobotch','writer' => 'XD TEAM',  'Results' =>$print_false_xd], 448);
    die;
}

try {
    if(strpos($get_url, '/reel/') || strpos($get_url, '/p/')) // post or reels
    {
        $ch = curl_init();
        $endpoint = "https://api3.haji-api.ir/sub/insta/post";
        $params = ['key' => 'FhSgxGRCTjqHzZphpoqGR8NBFf31YoR8', 'url'=>"$get_url"];
        $url = $endpoint . '?' . http_build_query($params);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux Centos 7;) Chrome/74.0.3729.169 Safari/537.36");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10000); // 10 sec
        curl_setopt($ch, CURLOPT_NOBODY, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        
        $end_url=curl_exec($ch);
        curl_close($ch);

        $serviceResultArray = json_decode($end_url,true);
        
        $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' => true, 'channel' => '@xdrobotch','writer' => 'XD TEAM',  'Results' =>$resultnext], 448);
    }
    elseif(strpos($get_url, '/stories/'))
    {
        $ch = curl_init();
        $endpoint = "https://api3.haji-api.ir/sub/insta/story";
        $params = ['key' => 'FhSgxGRCTjqHzZphpoqGR8NBFf31YoR8', 'url'=>"$get_url"];
        $url = $endpoint . '?' . http_build_query($params);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch,CURLOPT_NOBODY,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_AUTOREFERER,1);
        curl_setopt($ch,CURLOPT_ENCODING, 'UTF-8');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $end_url=curl_exec($ch);
        curl_close($ch);

        $serviceResultArray = json_decode($end_url,true);
        
        $resultnext = [
                'number'=> 0,
                0 =>[
                    'link' => $serviceResultArray['result']['media'],
                    'caption' => $serviceResultArray['result']['caption']
                ]
            ];
        
        if($test) {
             var_dump($resultnext);
             die;
        }

        echo json_encode(['ok' => true, 'channel' => '@xdrobotch','writer' => 'XD TEAM',  'Results' =>$resultnext], 448);
    }
    elseif(strpos($get_url, '/s/'))  //highlight
    {
        $ch = curl_init();
        $endpoint = "https://api3.haji-api.ir/sub/insta/highlight";
        $params = ['key' => 'FhSgxGRCTjqHzZphpoqGR8NBFf31YoR8', 'url'=>"$get_url"];
        $url = $endpoint . '?' . http_build_query($params);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch,CURLOPT_NOBODY,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_AUTOREFERER,1);
        curl_setopt($ch,CURLOPT_ENCODING, 'UTF-8');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $end_url=curl_exec($ch);
        curl_close($ch);

        $serviceResultArray = json_decode($end_url,true);
        
        $resultnext = [
                'number'=> 0,
                0 =>[
                    'link' => $serviceResultArray['result']['media'],
                    'caption' => ''
                ]
            ];
        
        if($test) {
             var_dump($resultnext);
             die;
        }

        echo json_encode(['ok' => true, 'channel' => '@xdrobotch','writer' => 'XD TEAM',  'Results' =>$resultnext], 448);
    }
}
catch (Exception $ex) {
    $print_false_xd = array();
    $print_false_xd['res']['text']= "خطا در خواندن اطلاعات ، لینک ورودی را چک کنید یا مجددا امتحان کنید";
    echo json_encode(['ok' => false, 'channel' => '@xdrobotch','writer' => 'XD TEAM',  'Results' =>$print_false_xd], 448);
    exit;
}
