2009-08-14 43 views
9

我正在开发一个php脚本来抓取这个网站并将数据通过电子邮件发送给我。它似乎正确登录,因为脚本运行时,它似乎重定向,并给我一个消息,说对象移到这里,这里是链接到default.aspx页面,这是什么时候我手动登录。php ssl curl:object moved error

下面是我的脚本:

<?php 
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; 

// INIT CURL 
$ch = curl_init(); 

//init curl 
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 

// SET URL FOR THE POST FORM LOGIN 
curl_setopt($ch, CURLOPT_URL, 'https://access.manager.com/Login.aspx'); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 

// Set your login and password for authentication 
curl_setopt($ch, CURLOPT_USERPWD, 'testu:passwd'); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 


// This is occassionally required to stop CURL from verifying the peer's certificate. 
// CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if 
// CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2 - check the existence of a 
// common name and also verify that it matches the hostname provided) 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

// Optional: Return the result instead of printing it 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// ENABLE HTTP POST 
curl_setopt ($ch, CURLOPT_POST, 1); 

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD 
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'Username=testu&Password=passwd'); 

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL 
# not to print out the results of its query. 
# Instead, it will return the results as a string return value 
# from curl_exec() instead of the usual true/false. 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 

// EXECUTE 1st REQUEST (FORM LOGIN) 
$store = curl_exec ($ch); 
echo $store; 
// CLOSE CURL 
curl_close ($ch); 

?> 

任何想法可能会阻止页面正确输出的页面?我猜测它没有正确重定向。

在此先感谢

回答

11
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 

修改成1。此外,CURLOPT_SSL_VERIFYPEER后设置此:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
+0

真棒!修复它。 – phill 2009-08-14 19:41:01

+0

另外,看看CURLOPT_MAXREDIRS你可能不得不使用它(将来?)。 – 2009-08-14 19:59:53

+1

为什么?为什么用户应该添加/更改这些? – 2017-12-02 21:37:31

2

您可以使用下面:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_MAXREDIRS,5); // return into a variable 
+0

@ jeffrey-roosendaal感谢您的编辑。 – 2016-10-06 07:47:36