【问题标题】:Why the below PHP cURL code is not POSTing any data?为什么下面的 PHP cURL 代码没有发布任何数据?
【发布时间】:2020-09-14 11:04:01
【问题描述】:

谁能告诉我在下面的代码中做错了什么?

我正在编写 php 脚本以从 https://beams.us.yazaki.com/BEAMSLogin/Login.aspx?ReturnUrl=%2FBeams%2FDefault.aspx 登录和抓取数据

我已尝试使用以下 curl 代码。

<?php
define('USERNAME', 'XXX');
define('PASSWORD', 'XXX');
define('DOMAIN', 'XXX');
define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
define('COOKIE_FILE', 'cookie.txt');
define('LOGIN_FORM_URL', 'https://beams.us.yazaki.com/BEAMSLogin/Login.aspx?ReturnUrl=%2fBeams%2fDefault.aspx');
define('LOGIN_ACTION_URL', 'https://beams.us.yazaki.com/BEAMSLogin/Login.aspx?ReturnUrl=%2fBeams%2fDefault.aspx');
$postValues = array(
    'txtUserName' => USERNAME,
    'txtPassword' => PASSWORD,
    'lstDomain'   => DOMAIN
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, LOGIN_ACTION_URL);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, LOGIN_FORM_URL);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_exec($curl);
if(curl_errno($curl)){
    throw new Exception(curl_error($curl));
}
curl_setopt($curl, CURLOPT_URL, 'https://beams.us.yazaki.com/BEAMS/SearchAndResults.aspx?topic=component');
curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
echo curl_exec($curl);
?>

我得到如下页面的输出。

Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. 

If you think this is a server error, please contact the webmaster. 

Error 404

localhost
Apache/2.4.43 (Win64) OpenSSL/1.1.1g PHP/7.4.8 *

浏览器被重定向到 http://localhost/BeamsLogin/AuthenticationSelect.aspx?ReturnUrl=%2fBEAMS%2fSearchAndResults.aspx%3ftopic%3dcomponent&topic=component

我正在认真等待回复。

谢谢。

编辑 1 @Shibon 我认为这个网站不使用任何 csrf 令牌。我正在提供 HTTP 标头实时内容。请检查一次。

https://beams.us.yazaki.com/BEAMSLogin/Login.aspx?ReturnURL=%2fBeams%2fDefault.aspx
Host: beams.us.yazaki.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://beams.us.yazaki.com/
Connection: keep-alive
Upgrade-Insecure-Requests: 1

GET: HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 15 Sep 2020 06:19:19 GMT
Content-Length: 3376

【问题讨论】:

  • 我认为这与 cookie 有关。首先尝试获取网站,然后尝试登录,您还应该查看表单中的 __VIEWSTATE。
  • 我尝试使用$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST); print_r($cookies); 获取并得到 Array() 作为输出。我认为你是对的。你能建议如何解决这个问题,因为我对此有点陌生。
  • 谢谢@nguaman,我应该发送 __VIEWSTATE、__EVENTVALIDATION、__VIEWSTATEGENERATOR 以及用户名和密码。

标签: php curl web-scraping


【解决方案1】:

试试下面的代码

$ch= curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEJAR, "-"); //to set session and cookie for the current $ch object
curl_setopt($ch, CURLOPT_URL, LOGIN_ACTION_URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postValues));
$resp = curl_exec($ch);
if(!$resp) {
   print_r(curl_error($ch));
   exit;
}
curl_setopt($curl, CURLOPT_URL, 'https://beams.us.yazaki.com/BEAMS/SearchAndResults.aspx?topic=component');
curl_setopt($ch, CURLOPT_POST, true);
echo curl_exec($ch);

在此设置了 cookie 并在所有请求中使用了该 cookie

【讨论】:

  • 谢谢@Shibon,我试过了。但是同样的错误。基本上,我不知道为什么浏览器会被重定向到Link。登录表单的源页面中未提及此链接。
  • 你能不能试试这样beams.us.yazaki.com/BEAMSLogin/Login.aspx987654322@
  • 否;它仍然被重定向到相同的(Authentication.aspx)页面。基本上它是在我的本地主机服务器上搜索这个链接。早些时候我尝试使用简单的 HTML dom 解析器,通过它我可以发送表单数据但无法按下登录按钮,现在我知道了;没用。还有一件事;此表单的源页面具有 (./Login.aspx) 之类的操作链接。那么如何将其重定向到相同的 https 地址而不是我的本地主机上?我认为这是个问题。
  • 所以你的第一个 curl 是成功的,但它正在重定向是它..
  • 是的。我让表单可见,但不是在登录后将链接定向到组件搜索;我被定向到源代码中未提及的某个链接,我不知道 POST 是否已发送任何数据并且我的登录是否成功。
猜你喜欢
  • 2017-03-21
  • 2013-01-24
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多