简体   繁体   English

使用 PHP 发送 POST 表单数据的问题:cURL

[英]Issue with sending POST form data with PHP : cURL


Below are 2 pieces of code whose result should be identical (at least I would like).下面是 2 段代码,其结果应该是相同的(至少我愿意)。 This result consists in logging in to a website in one click by sending a form.此结果包括通过发送表单一键登录网站。
The HTML one works well and allows me to connect perfectly. HTML 运行良好,让我可以完美连接。
The PHP one gives me a hard time. PHP 给我带来了困难。 The PHP/cURL one runs correctly, I mean if I change Login/Password variables, the server sends me back "wrong user/password", but if I send the correct Login/Password, the server turns in deep meditation and never answers me. PHP/cURL 运行正确,我的意思是如果我更改登录/密码变量,服务器会向我发送“错误的用户/密码”,但是如果我发送正确的登录/密码,服务器会陷入沉思并且从不回答我.

Do any of you have any suggestions for this behaviour ?你们对这种行为有什么建议吗? (I certainly would have missed something...). (我肯定会错过一些东西......)。
Thanks谢谢

«AutoConnect.HTM»: «AutoConnect.HTM»:

<html>
<script type="text/javascript">
window.onload = function () { document.getElementById('autolog').submit(); }
</script>
<body>
<form id="autolog" action="https://website.com/account/logon" method="post">
  <input name="Login" value="User">
  <input name="Password" value="Pass">
</form>
</body>
</html>

«AutoConnect.PHP»: «AutoConnect.PHP»:

<?php
 $url = 'https://website.com/account/logon';
 $postinfo = http_build_query(array(
         'Login'    => 'User',
         'Password' => 'Pass'
         ));
 $client = 'Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)';
 $header = array(
        'Connection: Keep-Alive',
        'Accept: */*',
        'Accept-Language: fr-FR',
        'Content-type: application/x-www-form-urlencoded',
        'Content-Length: '.(strlen($postinfo))
        );
 $options = array(
        CURLOPT_HTTPHEADER     => $header,
        CURLOPT_HEADER         => false,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_USERAGENT      => $client,
        CURLOPT_AUTOREFERER    => true,
        CURLOPT_POST           => 1,
        CURLOPT_POSTFIELDS     => $postinfo,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_URL            => $url
        );
 $ch = curl_init();
 curl_setopt_array($ch,$options);
 $result = curl_exec($ch); 
 curl_close($ch);
 echo $result;
?>

After wandering the web for hours, I did it!在网上游荡了几个小时后,我做到了!
Solution was there on stackoverflow, but I was missing a key word.解决方案在stackoverflow上,但我错过了一个关键词。
The server I'm trying to connect to is an «ASP» one with ASPXAUTH.我尝试连接的服务器是带有 ASPXAUTH 的 «ASP» 服务器。
The use of cookies had to be integrated.必须整合 cookie 的使用。 I give you here the final PHP code:我在这里给你最终的 PHP 代码:

<?php
$url   = 'https://website.com/account/logon';
$post  = http_build_query(array(
         'Login'    => 'User',
         'Password' => 'Pass'
         ));
$client = 'Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)';
$ckfile= 'MyCookie.txt'; 
$options1= array(
        CURLOPT_HEADER         => 0,
        CURLOPT_SSL_VERIFYPEER => false,   // accepts all certificats
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_USERAGENT      => $client,
        CURLOPT_COOKIEJAR      => $ckfile, // cookie needed 
        CURLOPT_RETURNTRANSFER => true,    // returns string
        CURLOPT_FOLLOWLOCATION => true
        );
$options2= array(
        CURLOPT_COOKIEFILE     => $ckfile, // cookie used
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $post    // form datas
        );
$ch      = curl_init($url);
curl_setopt_array($ch,$options1);
$output = curl_exec($ch); 
curl_setopt_array($ch,$options2);
$output = curl_exec($ch); 
echo $output;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM