How do I navigate to different pages after logging in via cURL? - php

Login page - https://b2b.chiemsee.com/customer/account/login/
After successful login page - https://b2b.chiemsee.com/customer/account/loginPost/
Page I want to go to - https://b2b.chiemsee.com/damen
I have made 3 separate requests:
1) To get the CSRF token.
2) To make the login.
3) To navigate to the url I want to go to.
Until I added the third request, the login was working fine and taking me to loginPost/ page but now it takes me back to the login/ page. Does it have something to do with the cookie in the third request?
PHP:-
// login request one to get form_key
include('simple_html_dom.php');
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/login/");
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); // to get the html
$response2 = curl_exec($ch2);
curl_close($ch2);
$html = new simple_html_dom();
$html->load($response2);
$val = $html->find('input[name=form_key]');
$form_key = $val[0]->value;
// login request two to send form_key, username and password
$data = array(
'form_key' => $form_key,
'login[username]' => 'user',
'login[password]' => 'pass',
'send' => 'Anmelden'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/loginPost/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch, CURLOPT_POST, true); // to send info
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // to get the html
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
curl_close($ch);
// request three to open first category
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL, "https://b2b.chiemsee.com/damen");
curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch3, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true); // to get the html
$response3 = curl_exec($ch3);
if (curl_error($ch3)) {
$error_msg3 = curl_error($ch3);
var_dump($error_msg3);
exit;
}
curl_close($ch3);
echo $response3;

I figured out the problem.
I wasn't saving the new data (username and password) sent in second request, in the cookie file, with:
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
Updated code:
// login request one to get form_key
include('simple_html_dom.php');
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/login/");
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); // to get the html
$response2 = curl_exec($ch2);
curl_close($ch2);
$html = new simple_html_dom();
$html->load($response2);
$val = $html->find('input[name=form_key]');
$form_key = $val[0]->value;
// login request two to send form_key, username and password
$data = array(
'form_key' => $form_key,
'login[username]' => 'user',
'login[password]' => 'pass',
'send' => 'Anmelden'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/loginPost/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // to avoid error
curl_setopt($ch, CURLOPT_POST, true); // to send info
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // to get the html
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
curl_close($ch);
//echo $response;
// request three to open first category
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL, "https://b2b.chiemsee.com/damen");
curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch3, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true); // to get the html
$response3 = curl_exec($ch3);
if (curl_error($ch3)) {
$error_msg3 = curl_error($ch3);
var_dump($error_msg3);
exit;
}
curl_close($ch3);
echo $response3;

2) To make the login.
that code never worked in the first place, you just have insufficient login-error-checking code to realize it.
replace
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
curl_close($ch);
with
$response = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
var_dump($error_msg);
exit;
}
$domd=#DOMDocument::loadHTML($response);
$xp=new DOMXPath($domd);
$login_errors=array();
foreach($xp->query("//*[contains(#class,'error-msg')]") as $login_error){
$login_error=trim($login_error->textContent);
if(!empty($login_error)){
$login_errors[]=$login_error;
}
}
if(!empty($login_errors)){
throw new \RuntimeException("login errors: ".print_r($login_errors,true));
}
curl_close($ch);
and you'll see that you get an error during login.

Related

How to send data to another page via php

I want to send some data to another server but I don't want user to view the data.
I tried submitting form but others still manage to see the data even it is hidden.
PS: The server only receives $_POST.
You use curl for this purpose.
An example:
<?PHP
function POST($url,$data,$headers,$type)
{
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_POST, 1);
if($type === 1)
{
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
}
else
{
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$output = curl_exec ($ch);
$posturl = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$cURLinfo = curl_getinfo($ch);
curl_close($ch);
return array(
"output" => $output,
"posturl" => $posturl,
"httpcode" => $httpCode,
"diagnostics" => $cURLinfo
);
}
?>
But most likely, this is a duplicate of How do I send a POST request with PHP?
use forge.js to encrypt the data in base64 e.g.
jQuery.post('yourfile.php',
{uname: forge.util.encode64(jQuery('#uname').val()) ,
upass: forge.util.encode64(jQuery('#upass').val() )
},function(data)
{
console.log(data);
});
and then in PHP you can use
$user = base64_decode($_POST['uname']);
$passw = base64_decode($_POST['upass']);
to retrieve the data.

Login to site and redirect to file download

I've managed to login to a site via curl, but now I would like to redirect to a file to download. This file requires a login cookie to download. How would I go about redirecting and using my previously saved cookie? I tried to set the url below after the login was successful but that didn't work.
<?php
$username = "user";
$password = "pass";
$url = "https://www.example.com/login";
$urldl = "https://www.example.com/get-download/abc.xyz";
$cookie = "cookie.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) die(curl_error($ch));
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($response);
$token = $doc->getElementById("csrf_token")->attributes->getNamedItem("value")->value;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POST, true);
$params = array(
'username' => $username,
'password' => $password,
'csrf_token' => $token
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_exec($ch);
if (curl_errno($ch)) print curl_error($ch);
curl_setopt($ch, CURLOPT_URL, $urldl);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
You've got CURLOPT_RETURNTRANSFER enabled, but you're not actually getting the value from curl_exec
// I want the result returned from exec
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// but I'm not gonna do anything with it...
curl_exec($ch);
So you should assign that to a value, set the header appropriately, and then print the response.
$result = curl_exec($ch);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="foo.bar"');
echo $result;

getting json file behind login page with curl

I'm trying to get json data from this page https://www.rbauction.com/rba-api/search/results/advanced?rbasq=YXJ8Tj00Mjk0MjM4MzMyKzQyOTQ5NjU3OTg=&offset=2&count=1&ccb=USD
when i visit that page from browser it will ask me to login, when logged in it will display the json right away upon login. what need to do is get that json data with php using curl.
$username = 'myemail';
$password = 'mypass';
$loginurl = 'https://www.rbauction.com/myaccount';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginurl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '_58_login='.$username.'&_58_password='.$password);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $store = curl_exec($ch);
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_setopt($ch, CURLOPT_URL, 'https://www.rbauction.com/rba-api/search/results/advanced?rbasq=YXJ8Tj00Mjk0MjM4MzMyKzQyOTQ5NjU3OTg%3D&offset=2&count=1&ccb=USD');
$store = curl_exec($ch);
var_dump($store);
when i do var_dump its just showing empty string

PHP cURL multiple form submit script

i'm trying to login and submit another contact form using PHP cURL.
Below is my script,
<?php
echo login_curl();
function login_curl() {
//First Form post data's
$postfields = array();
$postfields["formUsername"] = urlencode("xxx");
$postfields["formPassword"] = urlencode("xxx123");
//Define option variables
$action ="http://www.websss.com/websss/authenticate.php";
$cookie = "cookie.txt";
$agent = "Mozilla/26.0";
$referer = "http://www.websss.com/websss/login.php";
//Second form post data's
$secpostfields = array();
$secpostfields["form_url"] = urlencode("http://web.com");
$secpostfields["form_desc"] = urlencode("Jquery web Link");
//Define option variables
$secondaction ="http://www.websss.com/websss/insert_link.php";
$secondreferer = "http://www.websss.com/websss/login.php";
// Submit Login Form
$ch = curl_init($action);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//Capture output and close session
$result = curl_exec($ch);
curl_close($ch);
//submit second form after login
$secondch = curl_init($secondaction);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $secpostfields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $secondreferer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//Capture output and close session
$resultsecond = curl_exec($secondch);
curl_close($secondch);
return $resultsecond;
}
?>
I can note that, above script logging in properly but it's not submitting second form.
Did anyone know where i'm going wrong ?
Try don't close the session between first and second post.
And just modify url and post options you need to change
...
//Capture output and don't close session
$result = curl_exec($ch);
//curl_close($ch);
//submit second form after login
curl_setopt($ch, CURLOPT_URL, $secondaction);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $secpostfields);
curl_setopt($ch, CURLOPT_REFERER, $secondreferer);
$resultsecond = curl_exec($ch);

Logging in and downloading content using PHP and CURL

I'm trying to log in to site and then go to one of the pages and retrieve the data. I have a problem with authorization.
Sample code:
$loginUrl = 'https://strona.pl/authorization'; //action from the login form
$loginFields = array('username'=>'mail#mojmail.pl', 'password'=>'haslo'); //login form field names and values
$remotePageUrl = 'https://strona.pl/pdstrona'; //url of the page you want to save
$login = getUrl($loginUrl, 'post', $loginFields); //login to the site
$remotePage = getUrl($remotePageUrl); //get the remote page
function getUrl($url, $method='', $vars='') {
$ch = curl_init();
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookies.txt');
$buffer = curl_exec($ch);
//jeżeli pojawił się błąd to go wyświetlimy
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $buffer;
print $status;
}
// wyłącza pokazywanie błędów dla funkcji loadHTML
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom ->strictErrorChecking = FALSE;
$dom->loadHTML($remotePage);
$xpath = new DOMXpath($dom);
As a result of this query I am redirected to the main page (not the sub). What is important, I enter email address into a script in encoded form. Example: email% 40mojmail.pl
edit
Ok - I will add some information . I am a publisher and working with afilo.pl . Daily checking of rates is very tiring, so I wanted to prepare a script that will collect data once a day and informed me of the changes.
Unfortunately I can not retrieve the data.
Sample cookies from my browser :
Set- Cookie: PHPSESSID = jat102p33s0pmfairri1qiih24 ; expires = Thu, 25 -Dec- 2013 9:16:40 p.m. GMT ; path = / ; domain = . Afilo.pl
I modified the code but it still does not work.
loginUrl = 'https://opentrack.afilo.pl/logowanie'; //action from the login form
$loginFields = array('loginemail'=>'.......','loginhaslo'=>'........');
//login form field names and values
$remotePageUrl = 'https://opentrack.afilo.pl/partner/programy-lista'; //url of the page you want to save
$login = getUrl($loginUrl, 'post', $loginFields); //login to the site
$remotePage = getUrl($remotePageUrl); //get the remote page
function getUrl($url, $method='', $vars='') {
$ch = curl_init();
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
session_start();
$strCookie = session_name() . '=' . $_COOKIE[ session_name() ] . '; path=/; domain=.afilo.pl';
session_write_close();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies/cookies.txt');
//curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookies.txt');
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );
$buffer = curl_exec($ch);
//je¿eli pojawi³ siê b³¹d to go wyœwietlimy
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $buffer;
print $status;
}
This is the answer to my question. This code works well.
// options
$EMAIL = 'login';
$PASSWORD = 'haslo';
$cookie_file_path = "/cookies/cookies.txt";
$LOGINURL = "https://domainname.com/auth";
$agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";
$ch = curl_init();
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
$fields = array();
$fields['loginemail'] = $EMAIL;
$fields['loginhaslo'] = $PASSWORD;
$POSTFIELDS = http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
$result = curl_exec($ch);
$remotePageUrl = 'https://domainname.com/partner/programy-lista';
curl_setopt($ch, CURLOPT_URL, $remotePageUrl);
$result = curl_exec($ch);

Categories