2017-07-27 98 views
0

獲取一個員額從Instagram的網頁內容有沒有辦法讓Instagram的網頁the contents of an post使用簡單phpPHP - 使用簡單的PHP

我做了一些搜索,我發現這個腳本

$url = 'https://www.instagram.com/pagename/'; 
$str = file_get_contents($url); 
$count = 0; 
if(preg_match('#followed_by": {"count": (.*?)}#', $str, $match)) { 
    $count = $match[1]; // get the count from Regex pattern 
    } 
echo $count; 

但它只有一個追隨者有一個方法 得到Instagram的內容使用相同的概念嗎?

+0

目前還不清楚你問什麼。 「發佈數據」是指HTTP POST數據還是Instagram發佈的內容?無論如何,這似乎是一種不好的做事方式。有些圖書館和API通過網絡服務與Instagram進行互動 - Google。 – Andy

+0

我現在有API的,但我現在wana,如果有一個選項,像上面的例子 – M0ns1f

+2

你在上面的例子中做的是讀取一個URL的整個HTML源,然後尋找一些標記嘗試並提取一些內容。所以答案是肯定的,如果你能弄清楚如何提取你想要的特定位的內容。但這是一種糟糕而不可靠的方式 - 例如,如果Instagram更改了他們的頁面標記,您的應用程序將會中斷。使用合適的圖書館,並妥善處理,例如https://github.com/cosenary/Instagram-PHP-API – Andy

回答

0

我把他這是不可靠的@Andy建議和它的骯髒AF

所以這是我發現走了過來html

Instagram更改他們的頁面標記,您的應用程序將中斷。

是這個

$username = 'username'; 
    $instaResult= 
    file_get_contents('https://www.instagram.com/'.$username.'/media/'); 
    //decode json string into array 

    $data = json_decode($instaResult); 

foreach ($data as $posts) { 
    foreach($posts as $post){ 
    $postit = (array) json_decode(json_encode($post), True); 
    /* get post text and image */ 
       echo '<p>' .$postit["caption"]["text"].'</p>'; 
       echo '<img src="'.$postit["images"]["standard_resolution"]["url"].'" />'; 
       echo "</br>-----------</br>"; 
    } 
    } 
0

下面是一個有效的代碼(今天)。但正如@Andy說,這是不可靠的,它的骯髒AF :)

<?php 

$source = file_get_contents("https://www.instagram.com/p/POST_ID/"); 

preg_match('/<script type="text\/javascript">window\._sharedData =([^;]+);<\/script>/', $source, $matches); 

if (!isset($matches[1])) 
    return false; 

$r = json_decode($matches[1]); 

print_r($r); 

// Example to get the likes count 
// $r->entry_data->PostPage[0]->graphql->shortcode_media->edge_media_preview_like->count