PHP/cURL how to get Request Cookies - php

Hy,
I have a bit of a problem here. One client of mine asked to create a login script for a website built in ASP. As much as I can take and use Response Cookies, I am not able to take and use Request Cookies. I tried, but they are not saved in my cookie files. As such, I cannot login into the very website. Its address is http://www.itraceuk.co.uk/Default.asp?secure=signin
Here is the code I am currently using:
$ch4 = curl_init();
curl_setopt ($ch4, CURLOPT_URL, $posturl);
curl_setopt($ch4, CURLOPT_HEADER, true);
curl_setopt ($ch4, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch4, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch4, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36");
curl_setopt ($ch4, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch4, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch4, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch4, CURLOPT_COOKIESESSION, 1);
curl_setopt ($ch4, CURLOPT_COOKIEJAR, $cookie2);
curl_setopt ($ch4, CURLOPT_COOKIEFILE, $cookie2);
curl_setopt ($ch4, CURLOPT_REFERER, 'http://www.itraceuk.co.uk/Default.asp?secure=signin');
$result4 = curl_exec ($ch4);
curl_close($ch4);

<?php
$posturl = 'http://www.itraceuk.co.uk/Default.asp?secure=signin';
$cookie = __DIR__ . '/cookie.txt'; // file and folder must be writeable
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.itraceuk.co.uk/Default.asp?secure=signin');
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
var_dump(file_get_contents($cookie));
Set cookie folder/file permissions to writeable by PHP and Server.
Reuse the cookie data after the login.
Curl Post to the login form with correct credentials, that logs you in and sets the cookie (curl CURLOPT_POSTFIELDS). Then reuse this cookie for the second curl request (this code here).
For a complete example of a HTTPS cURL login, please see: https://stackoverflow.com/a/10307956/1163786

Related

cURL web scraping from VBulletin

Hi need some help i am new to cURL,i am trying to scrap data from http://www.ugbettingforum.co.uk,
so i can send any changes in forum to my e-mail.
But can't pass log-in. Forum is VBulletin and i manage to get massage "thanks for log in username" but after redirect i am not loged in.
here is my code:
function vBulletinLogin($user, $pass){
$md5Pass = md5($pass);
$data = "do=login&url=%2Findex.php&vb_login_md5password=$md5Pass&vb_login_username=$user&cookieuser=1";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, "http://www.ugbettingforum.co.uk/login.php?do=login"); // replace ** with tt
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0");
curl_setopt ($ch, CURLOPT_TIMEOUT, '10');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/cookie.txt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
echo $store;
curl_close($ch);}

Login with curl, save cookie file and visit page with cookie failed

I have a problem with PHP cURL login form. Ihave successefuly logged id the site and wrote my cookie in the file, than I visit the page again with the same cookie and it doesn't work... Please help!
My code for login is this:
$linkP = "https://www.something.com/login.php";
$postData = array(
'user' => 'something',
'pass' => 'something'
);
$userAgent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36';
$header = true;
$cookie = $_SERVER['DOCUMENT_ROOT'].'/tmp/cookie';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_URL, $linkP);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$result = curl_exec($ch);
curl_close($ch);
My cookie file contents are this:
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
.something.com TRUE / FALSE 0 stas 985mupc786rg6crikotnfj3oj3
My code for visting again is:
$cookie=$_SERVER['DOCUMENT_ROOT'].'/tmp/cookie';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $link);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
$html = curl_exec($ch);
echo $html;
It doesn't stay logged id. The other interesting thing is that if I login with browser manually and i take the hash from cookie and replace it with my generated cookie, than it works just fine. I don't see the point. Where is the difference?

Wget or curl download file - Object moved error 302

I prefer if this could be done using wget otherwise update my curl code. I have searched a lot and tried many suggested options like cookies, referer url, user-agent.
Here is my code so far:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.symop.com/config/includes/common/dw.asp?PF=Publications&NF=symop_guide2013.pdf');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.6 (KHTML, like Gecko) Chrome/16.0.897.0 Safari/535.6');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_REFERER, "http://www.symop.com/fr/Publications.asp");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$html = curl_exec($ch);
curl_close($ch);
echo $html;
302 Object Moved

Unable to login Google with curl

I want to login in google.com with php via curl but i am getting login page again and again.
Here is my code
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1 AlexaToolbar/alxg-3.1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($ch);
var_dump($result);
Also it is not creating the cookie file
I found this link Login to Google with PHP and Curl, Cookie turned off? . there is also the same code as mine but also i am unable to login.
What is wrong in this code ?

php curl cookie not registering

I've been playing with this curl facebook login script for a while just trying to get to grips with some of the features in curl, but it seems that I can not get the cookies to register:
php script
function facebookLogin(){
$login_email = 'email';
$login_pass = 'pass';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.facebook.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com");
$page = curl_exec($ch);
echo $page;
}
I have a text file called cookies.txt which is in the same directory as the script, but after running this script nothing is written into the file and therefore no cookies are created, this is a big issue when trying to explore other web pages on the same website as you have to keep logging in.
Where am I going wrong?
Ok it turns out it is registered even if the cookies.txt file is empty but you need to make sure you call this file when you try to explore other parts of the site e.g.
function facebookGoToMessages(){
facebookLogin();
$ch = curl_init ("http://www.facebook.com/messages");
curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com");
$page = curl_exec ($ch);
echo $page;
}

Categories