2013-11-26 18 views
0

我正在像我的網站實施鏈接共享功能。當用戶輸入一個url時,它會找到og:image標籤,或者使用一些算法爲該頁面找到合適的圖像。我也提取標題和描述。 我這樣做是通過使用get_file_contents()獲取文件內容然後提取東西。它適用於所有頁面執行fb頁面。 Facebook不讓我使用get_file_contents()無法提取任何Facebook頁面圖像使用php

任何方式,我可以做到這一點的Facebook?

+0

你的意思[file_get_contents()函數(http://us1.php.net/manual/en/function。文件得到-contents.php) – Ibu

回答

0

這違反了facebook條款。但這是你的問題的答案,Facebook有一個簡單的檢查,以確保頁面應該被瀏覽器打開,因此我們通過設置useragent頭來模仿它。

更好地與捲曲做到這一點: 這裏有一個類似的問題Facebook page feed doesn't work with php file_get_contents()

只需更改請求的URL。或者,如果你要堅持到文件獲取內容功能嘗試用戶代理的參數設置它:

$options = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Accept-language: en\r\n" . 
      "Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net 
      "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad 
) 
); 

$context = stream_context_create($options); 
$file = file_get_contents($url, false, $context); 
相關問題