2009-10-12 186 views
1

确定还是新的屏幕抓取的东西。PHP的屏幕抓取和会话

我已经设法登录到我需要的站点,但现在如何重定向到另一个页面? 我登录后,我试图在我需要的页面上执行另一个GET请求,但它有一个重定向,它将我带回登录页面。

所以我在想,SESSION变量没有被传递,我怎么能过来呢?

问题:

即使我上传它仍然重定向我回到登录页面,除非我已经在我登录第2页URL,但屏幕抓取代码不允许会话数据传递?

我发现这个代码another screen scraper question here @stack

class Curl { 

    public $cookieJar = ""; 

    public function __construct($cookieJarFile = 'cookies.txt') { 
     $this->cookieJar = $cookieJarFile; 
    } 

    function setup() { 
     $header = array(); 
     $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
     $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
     $header[] = "Cache-Control: max-age=0"; 
     $header[] = "Connection: keep-alive"; 
     $header[] = "Keep-Alive: 300"; 
     $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
     $header[] = "Accept-Language: en-us,en;q=0.5"; 
     $header[] = "Pragma: "; // browsers keep this blank. 

     curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7'); 
     curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header); 
     curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookieJar); 
     curl_setopt($this->curl, CURLOPT_COOKIEFILE, $cookieJar); 
     curl_setopt($this->curl, CURLOPT_AUTOREFERER, true); 
     curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); 
    } 

    function get($url) { 
     $this->curl = curl_init($url); 
     $this->setup(); 

     return $this->request(); 
    } 

    function getAll($reg, $str) { 
     preg_match_all($reg, $str, $matches); 
     return $matches[1]; 
    } 

    function postForm($url, $fields, $referer = '') { 
     $this->curl = curl_init($url); 
     $this->setup(); 
     curl_setopt($this->curl, CURLOPT_URL, $url); 
     curl_setopt($this->curl, CURLOPT_POST, 1); 
     curl_setopt($this->curl, CURLOPT_REFERER, $referer); 
     curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields); 
     return $this->request(); 
    } 

    function getInfo($info) { 
     $info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info); 
     return $info; 
    } 

    function request() { 
     return curl_exec($this->curl); 
    } 
} 

调用类

include('/var/www/html/curl.php'); 
$curl = new Curl(); 

$url = "here.com"; 
$newURL = "here.com/newpage.php"; 

$fields = "usr=user1&pass=PassWord"; 

// Calling URL 
$referer = "http://here.com/index.php"; 

$html = $curl->postForm($url, $fields, $referer); 
$html = $curl->get($newURL); 

echo $html; // takes me back to $url instead of $newURL 

回答

4

以下线不使用 “$ this” 和$ cookieJar没有在本地范围:

curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookieJar); 
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $cookieJar); 

因此,它应该是这样的:

curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieJar); 
    curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieJar); 

如果仍不能解决问题问题尝试只做岗位:

$curl->postForm($url, $fields, $referer);

,而不是

$curl->get($newURL)

然后检查cookie.txt文件包含什么?它是否被创建?让我们知道结果,因为很难快速测试您的代码而不打实际的URL。

如果没有创建cookie.txt文件,则几乎可以保证会话不会在请求之间保留。

+0

谢谢你的问题以及cookies.txt文件没有正确的权限。这样的新手错误。再次感谢 – 2009-10-12 17:33:34

0

也许例如心不是正确的..但是从它的域名是改变容貌..所以here.com会话不会存在there.com

+0

会话应该从here.com传递到there.com它是相同的域只是一个不同的页面,但只是例如我使用。也许我应该改变 – 2009-10-12 17:27:25

0

该网站可能试图将会话ID存储在cookie中。尽管你已经通过“cookies.txt”文件设置了curl来使用cookie。所以,我的第一个想法是 - cookies.txt文件里有什么?脚本是否有权限创建该文件?

0

这是工作正常使用$ curl-> get($ newURL)而不是$ curl-> postForm($ url,$ fields,$ referer);