Login to website using PHP and get text from page
Posted
by
Anthony Garand
on Stack Overflow
See other posts from Stack Overflow
or by Anthony Garand
Published on 2010-11-20T13:39:01Z
Indexed on
2013/07/03
5:06 UTC
Read the original article
Hit count: 467
I am trying to login to a website and grab content from a page you must be authenticated to see. I have done some research and have seen some examples using both cURL and stream_context_create but I cannot get either way to work. I have the url for the page to login to, and the page that contains the data I need to get. Your help is much appreciated!
Here's what I'm working with:
<?php
$pages = array('home' =>
'https://www.53.com/wps/portal/personal',
'login' =>
'https://www.53.com/wps/portal/personal',
'data' =>
'https://www.53.com/servlet/efsonline/index.html?Messages.SortedBy=DATE,REVERSE');
$ch = curl_init();
//Set options for curl session
$options = array(CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HEADER => TRUE,
//CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookies.txt');
//Hit home page for session cookie
$options[CURLOPT_URL] = $pages['home'];
curl_setopt_array($ch, $options);
curl_exec($ch);
//Login
$options[CURLOPT_URL] = $pages['login'];
$options[CURLOPT_POST] = TRUE;
$options[CURLOPT_POSTFIELDS] = 'uid-input=xxx&pw=xxx';
$options[CURLOPT_FOLLOWLOCATION] = FALSE;
curl_setopt_array($ch, $options);
curl_exec($ch);
//Hit data page
$options[CURLOPT_URL] = $pages['data'];
curl_setopt_array($ch, $options);
$data = curl_exec($ch);
//Output data
echo $data;
//Close curl session
curl_close($ch);
?>
Cheers,
Anthony
© Stack Overflow or respective owner