2013-11-03 39 views
1

我發現了一個提供屏幕捕獲服務的網站http://ctrlq.org/screenshots/。我想批量請求從多個URL獲取屏幕截圖,所以我已經編寫了一個PHP cURL腳本來以編程方式執行此操作。PHP cURL用正確的POST數據返回意外的結果

爲了從這個Web服務獲取屏幕截圖圖像,它需要POST數據以及需要捕獲的安全代碼和網站URL。

下面的代碼是捲曲的腳本,做上述兩項行動

$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 scr是否有錯誤IPT?謝謝。

回答

1

你忘了處理會話cookie。 使用

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

在一開始。