Reputation:
I am trying to make a login php post but its not posting correctly... i need the following sent to the Curl but its just not sending
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'j_username='.$username.'&j_password='.$password.'&tk_trp ='.$tk_trp);
Is what i am currently trying to use and its just not taking is there something anyone notices that i am doing wrong? Any advice would be great thanks!
I have been stuck on this a good while now and its just not coming together for me so thought i would reach out :)
<?php
$token = GetStringBetween(getURL("tokenlocatonurl"),"start'", "'");
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);
$username = 'myuser';
$password = 'mypass';
$tk_trp = '$token';
$loginUrl = 'lognurlhere';
function getURL($u){
$u = file_get_contents("http://{$u}");
return $u != false ? $u : "";
}
function GetStringBetween($string, $start, $finish){
$string = " ".$string;
$position = strpos($string, $start);
if ($position == 0) return "";
$position += strlen($start);
$length = strpos($string, $finish, $position) - $position;
return substr($string, $position, $length);
}
//init curl
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters curl_setopt($ch, CURLOPT_POSTFIELDS, 'j_username='.$username'&j_password='.$password'&tk_trp='.$tk_trp');
//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request (the login)
$store = curl_exec($ch);
//the login is now done and you can continue to get the
//protected content.
//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'url to grab content from');
//execute the request
$content = curl_exec($ch);
?>
Upvotes: 1
Views: 183
Reputation: 4370
Problems I see with your code are:
http_build_query
to encode your data.CURLOPT_COOKIESESSION
.CURLOPT_POST
AFTER you logged in.CURLOPT_COOKIEFILE
and CURLOPT_COOKIEJAR
and use '-'
to store it in memory.Here is final code:
<?php
$data = [
'j_username' => $username,
'j_password' => $password,
'tk_trp' => $tk_trp
];
$ch = curl_init("http://example.com/login");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, '-');
curl_setopt($ch, CURLOPT_COOKIEJAR, '-');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://example.com/dashboard");
curl_setopt($ch, CURLOPT_POST, 0);
$x = curl_exec($ch);
echo $x;
Upvotes: 1
Reputation: 46
Could it be that extra space in &tk_trp and = ? curl_setopt($ch, CURLOPT_POSTFIELDS, 'j_username='.$username.'&j_password='.$password.'&tk_trp ='.$tk_trp);
Upvotes: 0