简体   繁体   English

PHP cURL返回带有正确POST数据的意外结果

[英]PHP cURL return unexpected result with correct POST data

I found a website http://ctrlq.org/screenshots/ that providing screen capture service. 我找到了一个提供屏幕捕获服务的网站http://ctrlq.org/screenshots/ I would like to batch request the screen capture from many URL, so I have written a PHP cURL script to do that programmatically. 我想从许多URL批量请求截屏,因此我编写了一个PHP cURL脚本来以编程方式进行操作。

In order to get the screen capture image from this web service, it required the POST data with a secure code and an website URL that needed to be captured. 为了从此Web服务获取屏幕捕获图像,它需要带有安全代码的POST数据和需要捕获的网站URL。

The code below is the cURL script that doing the above two action 下面的代码是执行上述两个操作的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;

Unfortunately, I got the sources code: 不幸的是,我得到了源代码:

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

While the success code should be containing the screen capture image link: 尽管成功代码应包含屏幕截图图像链接:

<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>

I would like to ask is there any mistakes on this cURL script? 我想问一下这个cURL脚本是否有任何错误? Thanks. 谢谢。

You forgot to handle the session cookies. 您忘记了处理会话cookie。 Use 采用

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

at the very beginning. 一开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM