There is a website provided by our client that displays data I need for a report.
My client refused to build an API so I can retrieve the data I need as JSON, or even better provide read-only access to their DB.
So I thought, I can create a script that runs in a sever, that goes to the site, log in, search for an HTML table tag and retrieve data as an array in a loop, right?
Well, I do not know how to properly log in to the site using cURL.
The login form is as simple as two text boxes and a submit button, the form points to itself and once submitted redirects to another PHP file in the same server, that actually displays the data I need.
I search through the web and I did find a lot of great examples.
But when I try it, it returns a Boolean "True", and that is it.
$url = 'https://www.potato.site/login/login_file.php';
$fields = array(
'username' => urlencode('user'),
'password' => urlencode('password'),
'submit' => urlencode('Login')
);
$fields_string ="";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
I'm probably missing something here, I will greatly appreciate any help.
Thank you.
You need to set the CURLOPT_RETURNTRANSFER
option to get the contents of your request in $result
. Otherwise, curl returns true/false to indicate success.
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// $result now contains page contents