0

i am reading the html source code of instagram post by using the CURL. I am able to do this on localhost but when i test the code on live domain then meta tags with og property like og:type is missing, it only showing at localhost.

This is the complete code.

<?php

function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', 
    $domain, $regs)) {
    return $regs['domain'];
}
return false;
}

//run curl here and get html code of instagram post page
function file_get_contents_curl($url)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

$data = curl_exec($ch);
curl_close($ch);

return $data;
} 


//check instagram url
function checkinstaurl($urlhere) {

//remove white space
$urlhere = trim($urlhere);
$urlhere = htmlspecialchars($urlhere);
///remove white space

if (get_domain($urlhere) == "instagram.com") {

    //getting the meta tag data

    $html = file_get_contents_curl($urlhere);

    //parsing begins here:
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $nodes = $doc->getElementsByTagName('title');

    //get and display what you need:
    $title = $nodes->item(0)->nodeValue;

    $metas = $doc->getElementsByTagName('meta');
    $mediatype = null;
    $description = null;

    for ($i = 0; $i < $metas->length; $i++)
    {
        $meta = $metas->item($i);
        

        if($meta->getAttribute('property') == 'og:type')
            $mediatype = $meta -> getAttribute('content');

            if($mediatype == 'video') {
                if($meta->getAttribute('property') == 'og:video')
                    $description = $meta -> getAttribute('content');
                } else {
                    if($meta->getAttribute('property') == 'og:image')
                        $description = $meta -> getAttribute('content');
                        $mediatype = 'photo';
                    }

    } // for loop statement
    $out['mediatype'] = $mediatype;
    $out['descriptionc'] = $description;

    return $out;

    //getting the meta tag data
    }

}



    /*output*/
    
    $igurl = 'https://www.instagram.com/p/COf0dN0M8pU/';
    $output = checkinstaurl($igurl);
    echo "<pre>";
    print_r($output);

?>

This above code, At Localhost returns the complete html with meta tags but on live domain meta tags with og property is missing.

prieber
  • 554
  • 3
  • 16
  • Means i couldn't understand – Chetan Rohilla May 15 '21 at 03:07
  • Acually when i test this code on localhost, the curl returns html response from instagram post page url. But when i test this code on live server, the curl returns html response from instagram login page but i want html response from instagram post page url on live server also. – Chetan Rohilla May 15 '21 at 03:10
  • 1
    You have to login programmatically, until they allow anonymous access. Most sites (like Stack Exchange) allow anonymous access - Instagram is just being evil – Zombo May 15 '21 at 03:36
  • Related questions here which should help: https://stackoverflow.com/questions/50257082/how-can-i-login-to-instagram-using-php-and-curl-and-get-a-sessionid, https://stackoverflow.com/questions/59435149/get-instagram-public-json-feed-with-curl-php, https://stackoverflow.com/questions/39142374/request-instagram-api-acces-token-with-curl ... – Don't Panic May 15 '21 at 06:57

0 Answers0