Curl重定向到我自己的网站,而不是目标网站上的会员专用链接



我正试图从需要登录的网页中获取一些信息。我使用PHP/cURL将用户名和密码发布到目标网站的登录页面。该网站使用相对链接将经过身份验证的用户重定向到仅限会员的区域。

我得到了200 OK,我可以看到我正在成功地进行身份验证。我的问题是,我不知道如何将其转到目标网站(targetwebsite.com/memberarea)中的实际成员区域,而不是mywebsite.com/memberarea。有没有办法在cURL中指定目标网站的基本域?如果我正在做以下代码中不推荐的事情,你能告诉我吗。

以下是我正在做的。。。

<?php
// INIT CURL
$ch = curl_init();
// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'https://targetwebsite.com/login.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);
// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=someuser&password=mypassword');
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
$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($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com'); 
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
# 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, 0);
// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);

// SET FILE TO DOWNLOAD
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_URL, 'https://targetwebsite.com?memberarea+welcome+UserFirstName');

// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
$content = curl_exec ($ch);
//echo $store;
// CLOSE CURL
curl_close ($ch); 

?>

您是否收到Location:redirect?如果是,请使用CURLOPT_FOLLOWLOCATION

此外,Christian Sciberras建议将MAXREDIRECTS设置为至少3。证明

最新更新