Submit movie rating to imdb.com with curl - php

I am trying to log in to imdb.com with curl and then submit a rating for a movie. I'm aware that this goes against their ToS but I'm not building an application or anything, just a small script for personal usage. I'm new to curl but I got the login part working by using information found on stackoverflow. After I login I set the curl URL to http://www.imdb.com/ratings/_ajax/title, because that is where a rating is submitted. However, when I execute the curl commands, no rating is submitted. Not sure how to troubleshoot this either, so hopefully somebody can point me in the right direction? Here's what I got so far:
// options
$username = 'username';
$password = 'password';
$url_login = "https://secure.imdb.com/register-imdb/login";
$url_rating = "http://www.imdb.com/ratings/_ajax/title";
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$cookie_file_path = dirname(__FILE__)."/cookies.txt";
$agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
// get login page
$ch = curl_init();
// basic curl options for all requests
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_VERBOSE, 1);
// log
$verbose = fopen("loginfetch.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// set first URL
curl_setopt($ch, CURLOPT_URL, $url_login);
// execute session to get cookies and required form inputs
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
// grab the hidden inputs from the form required to login
$fields = getFormFields($return);
$fields['login'] = $username;
$fields['password'] = $password;
// set postfields using what we extracted from the form
$postfields = http_build_query($fields);
// post to login page
$ch = curl_init();
// set post options
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_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_URL, $url_login);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// log
$verbose = fopen("loginpost.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// perform login
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
//submit rating
$data['tconst'] = 'tt1709143';
$data['rating'] = '5';
$data['tracking_tag'] = 'title-maindetails';
$post = http_build_query($data);
// post to submit page
$ch = curl_init();
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_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url_rating);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// log
$verbose = fopen("ratingsubmit.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
After enabling logging I get three logs. The third one logs the request to the IMDB rating page, which does not work as you can reed in the log below:
* About to connect() to www.imdb.com port 80 (#0)
* Trying 72.21.203.211... * connected
* Connected to www.imdb.com (72.21.203.211) port 80 (#0)
> POST /ratings/_ajax/title HTTP/1.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36
Host: www.imdb.com
Cookie: cs=ECxJtiNrhA/m+SuIKh15AweBbbqgkVqM8BkNuqOS/TKzsu7Z84JeKeCRXRoA0U26oKcq7CWRbbqj9TkMh9HN2eCRWyxAGW26oKdbraCRbbqgsW26oJFt+uDBHYqg==; cache=BCYqeti-w3RKC8bV21R-BwArPk4ILOkGu0T6E1oB5KGihmddDp_kluyca1x7QLflsfnEZ9smi6EZc2uHo7eY5FZeXfG4EQ97tKKFR8VhyAW4d4Q; id=BCYiSjnWuGQc8HDlo5OAY8cDzxQyS5nHJqLgwq_9yI08DAjTU5l0CeOXL8dUvE28QUv1MNlBQ0MD5jEzs8OuhUVQKukg_AtlD58ORFostzT-mCzLCuv8a_mOFztCRGX7V3rpONDCl_xyKHAEj2JLSnWHI8VbKrpes93j5xsgNtdgeU0oYH3s93XMeRVWOM06V1Lg; session-id-time=1551966508; session-id=357-4286508-9576651; uu=BCYvAfd_f2bQLnYdtpdRlYkDth4AKSl6zlKVXzyzSLlagoM-bH3kvZe3FLFOj_KmoWbEkh-dRXiPZZtStWC72Dbsd6jCQiNnXDAyxc-_vmzg5yiJLuwbKVF6nICv9xuwCV_Gn-_Ek8gqTujYDQPdgIWR2Y3aXArES1RzXoqX1pA9jkZ1EkWFkVKNaukvSqxPQRJhE50xfMNMwaUJLJ8SLA1WRsIVLqp873yNvZf7ecyLd4hgmC7AxdbfzPtDCdwgaelx
Accept: */*
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 56
< HTTP/1.1 400 Bad Request
< Date: Sat, 08 Mar 2014 13:48:30 GMT
< Server: Server
< X-Frame-Options: SAMEORIGIN
< Content-Type: text/html;charset=UTF-8
< Content-Language: en-US
< Vary: Accept-Encoding,User-Agent
* Replaced cookie cache="BCYs4XPUvL_p2AL_pctQP7qEdwB9nBAXcIkiNxRZlqHtp9VjHkCy-GzvEIqsHCBHjjuGdWIyzZb1%0D%0Aip5WAl_SmYCtFg%0D%0A" for domain imdb.com, path /, expire 3541770158
< Set-Cookie: cache=BCYs4XPUvL_p2AL_pctQP7qEdwB9nBAXcIkiNxRZlqHtp9VjHkCy-GzvEIqsHCBHjjuGdWIyzZb1%0D%0Aip5WAl_SmYCtFg%0D%0A; Domain=.imdb.com; Expires=Thu, 26-Mar-2082 17:02:38 GMT; Path=/
< P3P: policyref="http://i.imdb.com/images/p3p.xml",CP="CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC "
< Cneonction: close
< Transfer-Encoding: chunked
<
* Connection #0 to host www.imdb.com left intact
* Closing connection #0

I figured out what went wrong. There was a missing string that IMDb expects to be submitted along with the movie ID and rating. It's called "Auth" and it is a string that is present on the movie movie page. So I added a function that looks for the auth string and pass this when submitting a rating to the IMDb. No more error.
Here's the whole (working) thing in case anyone is interested:
// options
$username = 'username';
$password = 'password';
$url_login = "https://secure.imdb.com/register-imdb/login";
$url_rating = "http://www.imdb.com/ratings/_ajax/title";
$movie_id = "tt1800241";
$url_movie = "http://www.imdb.com/title/" . $movie_id;
$data['tconst'] = $movie_id;
$data['rating'] = '7';
$data['tracking_tag'] = 'title-maindetails';
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$cookie_file_path = dirname(__FILE__)."/cookies.txt";
$agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
/**
Step 1: get login page and cookies
**/
$ch = curl_init();
// basic curl options for all requests
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);
// log
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$verbose = fopen("loginfetch.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// set URL
curl_setopt($ch, CURLOPT_URL, $url_login);
// execute session to get cookies and required form inputs
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
/**
Step 2: post login credentials
**/
// grab the hidden inputs from the form required to login
$fields = getFormFields($return);
$fields['login'] = $username;
$fields['password'] = $password;
// set postfields using what we extracted from the form
$postfields = http_build_query($fields);
// post to login page
$ch = curl_init();
// set post options
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_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
// log
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$verbose = fopen("loginpost.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// set URL
curl_setopt($ch, CURLOPT_URL, $url_login);
// perform login
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
/**
Step 3: get Auth string from movie page
**/
$ch = curl_init();
// basic curl options for all requests
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);
// log
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$verbose = fopen("authfetch.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// set URL
curl_setopt($ch, CURLOPT_URL, $url_movie);
// execute session
$return_auth = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return_auth;
/**
Step 4: submit rating
**/
$data['auth'] = getAuth($return_auth);
$post = http_build_query($data);
// post to submit page
$ch = curl_init();
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_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// log
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$verbose = fopen("ratingsubmit.txt", 'a+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// set URL
curl_setopt($ch, CURLOPT_URL, $url_rating);
// execute session
$return = curl_exec($ch);
// close connection
curl_close($ch);
//echo $return;
function getFormFields($data)
{
if (preg_match('/(<form method="post.*?<\/form>)/is', $data, $matches)) {
$inputs = getInputs($matches[1]);
return $inputs;
} else {
return('Login form not found.');
}
}
function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}
// when submitting a rating to IMDb you also need to send an 'auth' string which we grab from the rating-list div on the movie details page
function getAuth($data)
{
if (preg_match('/data-auth="(.*?)"/is', $data, $matches)) {
$auth = $matches[1];
return $auth;
} else {
return('Auth string not found.');
}
}

Your cookie is empty when you submit the request. You need to close the curl handle every time you do curl_exec:
curl_close($ch);
This will store the cookies into the file.
So for your code you'll need to close it three times. Make sure you are initializing the curl again after closing, and also make sure you are pointing the cookie file accordingly every time.

Related

curl hitting the url but data not recieved by server

Can anyone help me? I am tring to hit a website using curl.
My code is as follows:
$data = array(
'utm_source'=>'Google',
'utm_medium'=>'Google',
'utm_campaign'=>'Sales',
'utm_term'=>'united flights tickets'
);
$data = http_build_query($data, '', '&');
$proxies[] = 'user:password#71.6.46.151:443';
$proxies[] = 'user:password#14.102.19.206:61217';
$proxies[] = 'user:password#187.95.230.65:8080';
$url = 'https://www.example.com/index.php?'.$data;
$ch = curl_init();
if (isset($proxy)) { // If the $proxy variable is set, then
curl_setopt($ch, CURLOPT_PROXY, $proxy); // Set CURLOPT_PROXY with proxy in $proxy variable
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.google.com');
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
curl_setopt($ch, CURLOPT_URL, $url);
$results = curl_exec($ch); // Execute a cURL request
if (curl_error($ch)) {
$error_msg = curl_error($ch);
}
$info = curl_getinfo($ch);
curl_close($ch);
print_r($error_msg);
print_r($info);
print_r($results);
It hits the website and also displayed in the Google Analytics account, but the only problem is that the refferer and source showing "direct" in Google Analytics.
If there is any other way to achive this, please tell me.

curl login on Https asp page

I am trying to log into an .asp site with a https login page from PHP. I can't get logged into the site . There dosen't seem to be a cookie generated, viewstate, etc nor does it work leaving these parameters out. The form fields seem to fill correctly (i can physically see the login name) but im not sure about the password field which is a password type, but i don't think there is a issue there, its correctly spelled etc.
I have tried all the related posts including http://www.mishainthecloud.com/2009/12/screen-scraping-aspnet-application-in.html?showComment=1368565341638#c9104469935977149435
My code is below and returning "not Found" (error code 7 i think..) on the final call. No curl errors are present on the 1st two calls.
Can anyone help with this?
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
// URL to login page
$url = "https://secure2.clubwise.com/glenview/memberlogin.asp";
// Get Login page and its cookies/ viewstate , etc
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); // no cookie stored
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$output = curl_exec($ch);
//print(esc_html($output));
$viewstate = regexExtract($output,$regexViewstate,$regs,1);
$eventval = regexExtract($output, $regexEventVal,$regs,1);
// rebuild post info -- view state and eventvalidate empty! cant find on page
$fields_string =
'__VIEWSTATE='.rawurlencode($viewstate).
'&__EVENTVALIDATION='.rawurlencode($eventval).
'&login='.urlencode('xxx#xxx.com').
'&password='.urlencode('xxxx').
'&submit='.urlencode('Sign in').
'&redirect=';
echo $fields_string;
// Post login form -- password field ok?
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 5);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Tells cURL to follow redirects
$outputb = curl_exec($ch);
print curl_error;
//var_dump($outputb);
$url = "https://secure2.clubwise.com/glenview/bookclass.asp";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$outputc = curl_exec($ch);
print curl_error;
var_dump($outputc);
header information:
//$header= array(
//'HTTP/1.1 200 OK',
//'Date: Mon, 09 Jun 2014 00:55:17',
//'GMT Server: Microsoft-IIS/6.0',
//'X-Powered-By: ASP.NET',
//'Content-Length: 21035',
//'Content-Type: text/html',
//'Set-Cookie: ASPSESSIONIDCCSBSTDC=IHJBDOOBIDMJDDOFLAOOBENL; path=/',
//'Cache-control: private'
//);
SOLUTION: it was curl_init() on each operation that was causing this to break. Viewstate, headers are not needed.
$ourFileName = get_stylesheet_directory()."/cookieFile.txt";
$ckfile = $ourFileName;
// URL to login page
$url = "https://secure2.clubwise.com/glenview/memberlogin.asp";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); // Stores cookies in the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output = curl_exec($ch);
//Cookie obtained, login
$fields_string =
'&redirect='.
'&login='.urlencode('vvv#xxx.com').
'&password='.urlencode('xxx').
'&submit='.urlencode('"Sign in"' )
;
//cookie in the header + header not required
/*
$cookielines =file($ckfile);
foreach($cookielines as $row) {
if($row[0] != '#') {
$cookie=$row;
}
}
$header= array( // not needed at moment
'HTTP/1.1 200 OK',
'Date: Mon, 09 Jun 2014 00:55:17',
'GMT Server: Microsoft-IIS/6.0',
'X-Powered-By: ASP.NET',
'Content-Length: 21035',
'Content-Type: text/html',
'Set-Cookie: $cookie; path=/',
'Cache-control: private'
);
*/
$url = "https://secure2.clubwise.com/glenview/memberlogin.asp";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Tells cURL to follow redirects
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_REFERER,"https://secure2.clubwise.com/glenview/memberlogin.asp");
$outputb = curl_exec($ch);
$err = curl_errno ( $ch ); echo "<br>error=".$err;
$errmsg = curl_error ( $ch ); echo "<br>errmsg=".$errmsg;
$header = curl_getinfo ( $ch ); echo "<br>header="; var_dump($header);
$httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); echo "<br>httpcode=".$httpCode;
print curl_error;
//Now you should be able to access any pages within the password-restricted area by just including the cookies for each call:
$url = "https://secure2.clubwise.com/glenview/bookclass.asp?Mode=Area&RecId=67";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0 );
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$outputc = curl_exec($ch);
print curl_error;
var_dump($outputc);
Please check again that cookie stored on first page. Set CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR for Session cookies and not reinit or close curl for save a session.
Set CURLOPT_REFERER, some sites check it for login page.

php cURL LOGIN does not work

Now I am trying to login to my account using PHP and CURL, but still I am not sure why is not working.
The page I'm trying to login to is: http://lardi-trans.com/index.jsp
After the execution of the script, I get the same page without authorization.
Thanks in advance!
<?php
$url = "http://lardi-trans.com/log/index.jsp";
$referer = "http://lardi-trans.com/index.jsp";
$user_agent = "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0";
$post = "action=/log/login.jsp&log=MYLOGIN&passwd=MYPASSWORD&enter=enter&onlog=%D0%92%D0%BE%D0%B9%D1%82%D0%B8";
$header [] = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header [] = "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3";
$header [] = "Accept-Encoding:gzip,deflate";
$header [] = "Accept-Language:en-US,en;q=0.5";
$user_cookie = "cookies.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING, "identity");
curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $user_cookie);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$page = curl_exec($ch);
curl_close($ch);
echo $page;
?>
You should set your $url variable to the value of the login form's action - http://lardi-trans.com/log/login.jsp

Proxy Authentication Required with cURL

I would like to use a cURL function, but I'm behind a proxy, so I get an HTTP/1.1 407 Proxy Authentication Required error...
This is the php code I use:
$proxy_user = 'Michiel';
$proxy_pass = 'mypassword';
$proxy_url = 'myproxyurl:port';
$proxy = true;
$service_url = "https://www.myapiurltocall.com";
$service_user = 'user:password:FO';
$service_pass = 'password';
$ch = curl_init($service_url);
// Set proxy if necessary
if ($proxy) {
curl_setopt($ch, CURLOPT_PROXY, $proxy_url);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_user.':'.$proxy_pass);
curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
}
// Set service authentication
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, "{$service_user}:{$service_pass}");
// HTTP headers
$headers['Authorization'] = 'Basic ' . base64_encode("$proxy_user:$proxy_pass");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
//WARNING: this would prevent curl from detecting a 'man in the middle' attack
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
And I don't know what I do wrong... How can I get around this error?
This is what I'm mostly using :
function curlFile($url,$proxy_ip,$proxy_port,$loginpassw)
{
//$loginpassw = 'username:password';
//$proxy_ip = '192.168.1.1';
//$proxy_port = '12345';
//$url = 'http://www.domain.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
This is what I use and it works perfectly:
$proxy = "1.1.1.1:12121";
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$url = "http://www.google.pt/search?q=anonymous";
$credentials = "user:pass"
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
curl_setopt($ch, CURLOPT_HTTP_VERSION,'CURL_HTTP_VERSION_1_1' );
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD,$credentials);
curl_setopt($ch, CURLOPT_USERAGENT,$useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$result=curl_exec ($ch);
curl_close ($ch);
Try to implement this in your function
function send_string($data) {
// pr($data);exit;
$equi_input="Your values";
// echo $equi_input; exit;
$agent = ""//Agent Setting for Netscape
$url = ""; // URL to POST FORM. (Action of Form)
//xml format
$efx_request=strtoupper($equi_input);
//echo $efx_request;exit;
$post_fields = "site_id=""&service_name=""&efx_request=$efx_request";
$credentials = ;
$headers = array("HTTP/1.1",
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic " . $credentials
);
$fh = fopen('/tmp/test.txt', 'w') or die("can't open file"); //File to write the header information of the curl request.
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_STDERR, $fh);
curl_setopt($ch, CURLOPT_POST, 1); // use this option to Post a form
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); // Pass form Fields.
curl_setopt($ch, CURLOPT_VERBOSE, '1');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '1');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch); // grab URL and pass it to the variable.
curl_close($ch); // close curl resource, and free system resources.
fclose($fh);
//echo '<pre>';print_r($array);echo '</pre>';exit;
if (!empty($result)) {
return $result;
} else {
echo "Curl Error" . curl_error($ch);
}
}
check request send 200ok ...
And check your secure certificate if you are using....
Thanks.
Assuming the $proxy_user:$proxy_pass are the correct values, if you are behind an office firewall the authentication credentials to be used would be that of a network administrator and not your own...I've encountered something very similar in my work environment and that was the case for me as my normal credentials didn't have the necessary privileges. The best way to ensure that this is the case however would be to ensure the code works in an open network with your proxy settings commented. Hope this helps!

CURL - cookie not enabled/session expired

I am trying to log into a website using PHP CURL. It all works fine on websites which doesn't require cookies and session but it doesn't seem to work with the websites which rquire you so here is my code I found this code over here
any help on this would be apritiated thanks
Code
<?php
// 1-Get First Login Page http://signin.ebay.com/aw-cgi/eBayISAPI.dll?SignIn
$ebay_user_id = "username"; // Please set your Ebay ID
$ebay_user_password = "password"; // Please set your Ebay Password
$cookie_file_path = "cookie.txt"; // Please set your Cookie File path
$LOGINURL = "__";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
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);
$result = curl_exec ($ch);
// curl_close ($ch);
// 2- Post Login Data to Page http://signin.ebay.com/aw-cgi/eBayISAPI.dll
$LOGINURL = "url";
$POSTFIELDS = 'postfiends';
$reffer = "url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
// curl_close ($ch);
print $result;
?>
Maybe you need more options:
// define some HTTP headers
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-type: application/x-www-form-urlencoded;charset=UTF-8";
// to GET add
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);
// to POST add
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
// check for errors before close
$result = curl_exec($ch);
if ($result === false)
{
echo curl_error($ch);
}
curl_close($ch);
Be sure that your $cookie_file_path file is writable (if Linux). Play with CURLOPT_SSL_VERIFYHOST and CURLOPT_SSL_VERIFYPEER.
http://signin.ebay.com/aw-cgi/eBayISAPI.dll not only expects username and password as post data, but other parameters as well, maybe you didn't take account of that. Use Net tab on firebug to see the parameters passed, and try to duplicate it.

Categories