我正在一個網站上工作,應該完全可見的用戶在Facebook.com被禁止他們的地方。所以我的Facebook像框插件不會出現在他們身上。 (爲了不本地化這個問題,假設我想繞過所有的客戶端防火牆,並像在我的網站(我的網站不禁止那裏)簡單的HTML插件顯示框插件)。獲取臉部像插件捲曲的facebook的內容
我的服務器可以訪問Facebook.com,我認爲我可以使用curl(在我的服務器的計算機中)獲得插件的內容,然後在我的網站的任何部分執行並顯示該頁面的內容爲簡單的HTML。所以,我只是寫了下面的腳本:
<?
$c = curl_init('https://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fstevejobs&width=292&height=258&colorscheme=dark&show_faces=true&border_color&stream=false&header=false');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.facebook.com', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
', 'Accept-Language: en-us,en;q=0.5', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Accept-Encoding: gzip, deflate'));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt(CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$html = curl_exec($c);
if (curl_error($c))
die(curl_error($c));
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
?>
令人驚訝上面的代碼工作的https://www.youtube.com(這是禁止有太)或https://www.google.com而不是與URL甚至乾脆https://www.facebook.com在我的服務器工作。
另一個問題:如果我使用https://www.youtube.com而不是Facebook.com,我仍然無法獲得在YouTube.com中使用的CSS文件或Javascript文件(因爲它們被禁止,客戶也無法下載它)。我只能看到文字和一些圖像。我也想讓curl自動獲取CSS和Javascript文件的內容。
我還使用YQL爲了從Facebook.com得到像框插件的內容,但我得到了以下結果:
YQL聲明:
select * from html where url = 'https://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fstevejobs&width=292&height=258&colorscheme=dark&show_faces=true&border_color&stream=false&header=false'
結果:
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
yahoo:count="0" yahoo:created="2011-11-11T11:41:10Z" yahoo:lang="en-US">
<diagnostics>
<publiclyCallable>true</publiclyCallable>
<url
error="Redirected to a robots.txt restricted URL: https://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fstevejobs&amp;width=292&amp;height=258&amp;colorscheme=dark&amp;show_faces=true&amp;border_color&amp;stream=false&amp;header=false"
execution-start-time="1" execution-stop-time="6"
execution-time="5" http-status-code="403"
http-status-message="Forbidden" proxy="DEFAULT"><![CDATA[https://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fstevejobs&width=292&height=258&colorscheme=dark&show_faces=true&border_color&stream=false&header=false]]></url>
<user-time>6</user-time>
<service-time>5</service-time>
<build-version>23377</build-version>
</diagnostics>
<results/>
</query>
它看起來像facebook.com的robots.txt存在一些問題。我應該提到上面的YQL聲明適用於其他網站(如https://www.youtube.com或https://www.yahoo.com)。
在此先感謝
設置主持人:是CURLOPT_HTTPHEADER是沒有必要的;它會自動爲您設置。如果客戶端不支持這兩種類型的壓縮,單向設置Accept-Encoding標頭有點不禮貌。Facebook可能會在請求中使用各種啓發式方法來試圖阻止機器人出局,而且這一請求與瀏覽器通過檢查的請求不夠相似。你確定你不需要cookie來實現這個功能嗎? – Dan