0

我正在尝试编写一个脚本,该脚本将允许我登录到在 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;
}


?>
4

3 回答 3

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);
于 2013-04-15T10:54:44.373 回答
0

您必须按照一些步骤使用 https 站点登录

导出证书。

转到浏览器中的站点(在我的示例中为 FF)。双击“锁定”图标 > 安全 > 查看证书 > 详细信息 > 导出。另存为 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");
于 2013-04-15T11:05:39.057 回答
0

尝试单独的 url 和数据。使用 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);
于 2013-04-15T10:48:53.660 回答