1

我正在开发一个网站,该网站应该在 Facebook.com 被禁止的地方对用户完全可见。所以我的 Facebook like box 插件不会出现在他们面前。(为了不本地化这个问题,假设我想绕过所有客户端防火墙,并在我的网站中将类似框插件显示为一个简单的 HTML(我的网站在那里没有被禁止))。

我的服务器可以访问 Facebook.com,我认为我可以使用 curl(在我的服务器的计算机中)获取插件的内容,然后在我的网站的任何部分执行并显示该页面的内容作为简单的 HTML。所以我只写了以下脚本:

<?
$c = curl_init('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');

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&amp;width=292&amp;height=258&amp;colorscheme=dark&amp;show_faces=true&amp;border_color&amp;stream=false&amp;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;amp;width=292&amp;amp;height=258&amp;amp;colorscheme=dark&amp;amp;show_faces=true&amp;amp;border_color&amp;amp;stream=false&amp;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&amp;width=292&amp;height=258&amp;colorscheme=dark&amp;show_faces=true&amp;border_color&amp;stream=false&amp;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.comhttps://www.yahoo.com)。

提前致谢

4

1 回答 1

2

您的代码中有错误:

1-在代码的所有部分中将 $c 更改为 $ch。

2-在 curl_exec 函数之后添加“echo $html”。

3-正如@Dan 在评论中提到的,CURLOPT_HTTPHEADER 不是必需的。只需将其删除。

4-设置 curlopt_cookiejar 不是必需的,但我总是用 curl 设置它。(只是为了确保一切正常)

5-删除之前<!DOCTYPE的所有内容以正确显示内容。

尝试以下代码:

$ch = 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_USERAGENT , 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_COOKIEJAR , "facebookcookies"); 
curl_setopt($ch, CURLOPT_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"); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$html = curl_exec($ch);

//remove everything before <!DOCTYPE
echo preg_replace('/^[^<!]*<!\s*/', '<!', $html);

if (curl_error($ch))
    die(curl_error($ch));

// Get the status code
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
于 2011-11-17T06:50:17.507 回答