我正在尝试编写一个脚本,允许我登录zencart上运行的站点的密码保护区域并以字符串形式获取html。到目前为止,它下载HTML页面,作为.html文件,但只下载页面的公共版本(即它没有成功登录)。它是HTTPS,我相信这可能是它无法工作的部分原因。我的脚本有什么问题我需要什么CURL设置(很少有关于PHP的CURL文档:()
<?php
$username = "xxxx@gmail.com";
$password = "xxxxx";
$securityToken = "b6afe5babdd1b6be234d1976586fb1f1";
$loginUrl = "https://www.xxxxxxx.com.au/index.php?main_page=login&action=process";
//init curl
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email_address='.$username.'&password='.$password.'&securityToken='.$securityToken);
//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie122.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, 1);
//execute the request (the login)
$store = curl_exec($ch);
//the login is now done and you can continue to get the
//protected content.
//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'http://www.xxxxxxx.com.au/index.php?main_page=product_info&products_id=1488');
//execute the request
$content = curl_exec($ch);
//save the data to disk
file_put_contents('test122.html', $content);
if(curl_error($ch)) {
$error = curl_error($ch);
echo $error;
}
?>
答案 0 :(得分:0)
尝试单独的网址和数据。使用http_build_query()。
$loginUrl = "https://www.xxxxxxx.com.au/index.php";
...
$args = array(
'main_page' => 'login',
'action' => 'process',
'email_address' => $username,
'password' => $password,
'securityToken' => $securityToken
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($args));
你可能需要:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
可能有用:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
答案 1 :(得分:0)
完整示例工作:
$ch = curl_init($url);
$headers = array();
//post
$post = 'a=b&d=c';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=utf-8';
$headers[] = 'Content-Length: ' . strlen($post);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
//if get
// $headers[] = 'Content-type: charset=utf-8';
$headers[] = 'Connection: Keep-Alive';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// Follow redirects
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);// Set referer on redirect
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);// Stop after x redirects
curl_setopt($ch, CURLOPT_TIMEOUT, 20);// Stop after x seconds
//username and password (if any):
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//https
$https = strpos($url, "https://");
if($https !== false){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
$response = curl_exec($ch);
curl_close($ch);
echo($response);
答案 2 :(得分:0)
您必须按照一些步骤登录https网站
导出证书。
转到浏览器中的站点(在我的示例中为FF)。双击“锁定”图标&gt;安全&gt;查看证书&gt;详细信息&gt;出口。保存为X.509。
将其上传到脚本可以看到的位置。
然后添加
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/path/to/certificateCA.crt");