【问题标题】:Get content of facebook like box plugin with curl使用 curl 获取 facebook like box 插件的内容
【发布时间】:2011-11-11 11:56:28
【问题描述】:

我正在开发一个网站,该网站应该在 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 )。

提前致谢

【问题讨论】:

  • 设置主机:不需要 CURLOPT_HTTPHEADER;它是自动为您设置的。如果客户端不支持这两种类型的压缩,单方面设置 Accept-Encoding 标头有点狡猾。可能是 Facebook 在请求中使用了各种启发式方法来尝试将机器人拒之门外,而这个请求与浏览器的请求不够相似,无法通过他们的检查。你确定你不需要 cookie 来工作吗?

标签: php facebook curl yql facebook-likebox


【解决方案1】:

你的代码有错误:

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

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

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

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

5-删除&lt;!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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多