1

我找到了一个提供截屏服务的网站http://ctrlq.org/screenshots/ 。我想从许多 URL 批量请求屏幕截图,所以我编写了一个 PHP cURL 脚本以编程方式执行此操作。

为了从该 Web 服务获取屏幕截图,它需要带有安全代码的 POST 数据和需要捕获的网站 URL。

下面的代码是执行上述两个动作的 cURL 脚本

$url = 'http://ctrlq.org/screenshots/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
preg_match('/<input type="hidden" name="labnol" value="(.*)" \/>/', $content, $match);
$postData = array(
    'labnol' => $match[1],
    'url' => 'http://www.google.com'
);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_exec($ch);
$content2 = curl_exec($ch);
curl_close($ch);

echo $content2;

不幸的是,我得到了源代码:

Sorry but the tool couldn't capture that web page. Please <a href='/screenshots/'>try another URL</a>.

虽然成功代码应包含屏幕截图图像链接:

<div class="animate" id="progressmeter">
                          <div class="progress progress-striped active">
                            <div class="bar" style="width: 0%;"></div>
                          </div>
                        </div>
                        <script>
                            var progress = setInterval(function() {
                            var $bar = $('.bar');
                            if ($bar.width()==300) {
                                clearInterval(progress);
                                $('.progress').removeClass('active');
                                document.getElementById('progressmeter').innerHTML = "<a class='btn btn-success btn-large' href='http://ctrlq.org/files/screenshots/0e82990496751f51e3329094b26bd4a1.png' target='_blank'><i class='icon-download icon-white'></i> Download Image</a>  <a class='btn-large btn btn-info' href='/screenshots/'><i class='icon-camera icon-white'></i> New Capture</a>";
                            } else {
                                $bar.width($bar.width()+30);
                            }
                            $bar.text($bar.width()/3 + 10 +  "%");
                            }, 1500);
                        </script>

我想问一下这个 cURL 脚本有什么错误吗?谢谢。

4

1 回答 1

1

您忘记处理会话 cookie。采用

curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");

一开始。

于 2013-11-03T12:14:52.223 回答