PHP cURL two pages are overlapped - php

Hi all I am using cURL for login to site and fetching some data after login. In my script I am able to get logged in but when I am displaying result it is getting both pages(e.g login and after login both in same variable).
Here is my code:
$username="abcd";
$password="123456";
$url="https://www.abcd.com/login";
$cookie="cookie.txt";
$url2 = "https://www.abcd.com/home";
$postdata = "username=".$username."&password=".$password;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/3.0.0.1");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); // <-- add this line
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
//echo $result;
// make second request
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_POST, 0);
$data = curl_exec($ch);
var_dump($data);
curl_close($ch);
On
var_dump($data)
Output
I am getting both pages 1.)https://www.abcd.com/login and 2.)https://www.abcd.com/home in same variable.
Can anyone help me out!
Thank you.

Related

PHP + cURL: Error: The state information is invalid for this page and might be corrupted.

I try to login to
https://reg.viatoll.pl/PreLogin.aspx?language=po
with CURL. On error I should get "Error verification code" - in Polish: "Nieprawidlowy kod weryfikacyjny" but I get
Error: The state information is invalid for this page and might be corrupted. System.Web at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) at...
I don't know what I do wrong. I notify that this is sth with __VIEWSTATE and __EVENTVALIDATION. If I open this page in Chrome and change sth in __VIEWSTATE or __EVENTVALIDATION before post data I get the same error.
My code below:
<?php
require_once('simple_html_dom.php'); // http://simplehtmldom.sourceforge.net/manual.htm
$cookie="cookie2.txt";
$url = 'https://reg.viatoll.pl/PreLogin.aspx?language=po';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIE, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_REFERER, $url);
$result = curl_exec($ch);
$html = str_get_html($result);
$vs = $html->find('#__VIEWSTATE')[0]->value;
$ev = $html->find('#__EVENTVALIDATION')[0]->value;
// sending data
$txtVerificationCode='asdasdas';
$txtUserName="UserName";
$txtPassword="Haselko123";
$url='https://reg.viatoll.pl/PreLogin.aspx?language=po';
$postdata = "__LASTFOCUS=&__EVENTTARGET=btnLogin&__EVENTARGUMENT=&__VIEWSTATE=".$vs."&__EVENTVALIDATION=".$ev."&txtUserName=".$txtUserName."&txtPassword=".$txtPassword."&txtVerificationCode=".$txtVerificationCode;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); // <-- add this line
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo "POST: " . $postdata . "<br><br><br>";
echo $result;
?>
Please anybody help :)

Login to wordpress using PHP and curl

I have browsed through numerous questions/answers related to php curl wordpress login but could not get any to work.
I have domainA.com with a webapp which stores a wordpress administration username and password. There is a login button which when clicked should login and redirect me to domainB.com/wp-admin on a new tab.
Using this code:
$username="admin";
$password="pword";
$url="http://domainB.com/";
$cookie="cookie.txt";
$postdata = "log=". $username ."&pwd=". $password ."&wp-submit=Log%20In&redirect_to=". $url ."wp-admin/&wordpress_test_cookie=WP+Cookie+Check";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url . "/wp-login.php");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url . "wp-admin/");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;
This works fine. I get loggedin, but my url remains http://domainA.com/webapp/index.php.
I want to be redirected to regular administration http://domainB.com/wp-admin
Next i tried:
$username="admin";
$password="pword";
$url="http://domainB.com/";
$cookie="cookie.txt";
$postdata = "log=". $username ."&pwd=". $password ."&wp-submit=Log%20In&redirect_to=". $url ."wp-admin/&wordpress_test_cookie=WP+Cookie+Check";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url . "/wp-login.php");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url . "wp-admin/");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
header('Location:'.$url.'wp-admin');
This does not seem to log me in.
I can not seem to find what is wrong.

Grabbing data from a website with cURL after logging in?

What I am trying to do is login to a website and then go and grab data from a table since they do not have an export feature. So far I've managed to login and it shows me the user homepage. However I need to navigate to a different page or somehow grab that page while still being logged in with curl.
My code so far:
$username="email";
$password="password";
$url="https://jiltapp.com/sessions";
$cookie="cookie.txt";
$url2 = "https://jiltapp.com/shops/shopname/orders";
$postdata = "email=".$username."&password=".$password;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
curl_close($ch);
As I mentioned i get access to the main user page, but I need to grab the contents of the $url2 variable, not $url. How can I accomplish something like that?
Thank you!
Once logged in, make a second request for the page that contains the data you are after.
For subsequent requets, you must set the option CURLOPT_COOKIEFILE which points to the same file as CURLOPT_COOKIEJAR. cURL will read cookies from this file and send them with the request.
$username="email";
$password="password";
$url="https://jiltapp.com/sessions";
$cookie="cookie.txt";
$url2 = "https://jiltapp.com/shops/shopname/orders";
$postdata = "email=".$username."&password=".$password;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); // <-- add this line
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
// make second request
$url = 'page you want to get data from';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
$data = curl_exec($ch);

Remote login to hootsuite via cURL

I am trying to remotely login to Hootsuite using cURL. I want to have the Hootsuite app showing in an iframe within my own application. I am using the following code, but it doesn't seem to work. I just get presented with the login page, but with no error re: login.
$username="xxx";
$password="yyy";
$url="https://hootsuite.com/login";
$cookie="cookie.txt";
$postdata = "loginInfo[email]=".$username."&loginInfo[password]=".$password;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
curl_close($ch);

Using PHP CURL to login to a remote web site

I'm trying to login to a remote web site using CURL, but can't seem to get it to work.
The page I'm trying to login to is: https://vp1-voiceportal.megapath.com/Login/
So far, I've tried the following:
$username="username";
$password="password";
$url="https://vp1-voiceportal.megapath.com/Login/servlet/com.broadsoft.clients.oam.servlets.Login";
$cookie="cookie.txt";
$postdata = "EnteredUserID=".$username."&password=".$password."&domain=&UserID=&rememberPass=";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
curl_close($ch);
EDIT: The URL you specified is wrong, it should be:
https://vp1-voiceportal.megapath.com/servlet/com.broadsoft.clients.oam.servlets.Login
And not:
https://vp1-voiceportal.megapath.com/Login/servlet/com.broadsoft.clients.oam.servlets.Login
It looks like you need to follow redirects and specify the cookie file (for reading), try:
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
curl_close($ch);
It's also a good practice so specify an absolute (and writable) path to the cookie file.

Categories