[SOLVED] Why the below PHP cURL code is not POSTing any data?

saaish

Member
Joined
Aug 14, 2020
Posts
16
Can anybody please tell what i am doing wrong in below code ?

I am working on php script to login and scrape data from BEAMS | Login

I have tried with Below curl code.

PHP:
<?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);
?>

I am getting the output like below page.

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 *



And the browser is getting redirected to http://localhost/BeamsLogin/AuthenticationSelect.aspx?ReturnUrl=/BEAMS/SearchAndResults.aspx?topic=component&topic=component

Basically, I dont know why the browser is getting redirected to AuthenticationSelect.aspx. This link is nowhere mentioned in source page of login form.

I am earnestly waiting for response.

Thanks.
 
And the browser is getting redirected to http://localhost/BeamsLogin/AuthenticationSelect.aspx?ReturnUrl=/BEAMS/SearchAndResults.aspx?topic=component&topic=component

Basically, I dont know why the browser is getting redirected to AuthenticationSelect.aspx. This link is nowhere mentioned in source page of login form.

Because you've hit a page which requires authentication to access. ASP will automatically redirect to a login page if a page is protected by an authorization guard.
 
I mean, why "$postValues" are not getting posted in the "LOGIN_FORM_URL" ? Or What else I am missing in CURL ?

One more thing is, AuthenticationSelect.aspx is not a login page. Login page is "LOGIN_FORM_URL".

Can you please suggest coz I am a bit new to this.
 
Why have you set the Referer header to the login page?

Or What else I am missing in CURL ?

I've never really used cURL, I tend to use PostMan if I need to build my own POST requests.

One more thing is, AuthenticationSelect.aspx is not a login page. Login page is "LOGIN_FORM_URL".

When I visit the page without authentication, it redirects me to the login form url. You should be get a redirect from authentication select to the login page.

$postValues = array(
'txtUserName' => USERNAME,
'txtPassword' => PASSWORD,
'lstDomain' => DOMAIN
);

I assume that you defined your constants in your script? Have you checked the request body using your browser's developer tools?
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top