POSTing Form Data and Storing Cookie File with PHP Curl - php

$process = curl_init();
$temporary_cookie_file = 'temporaryfiles/'.strtolower($user_general_information['Username']).'_cookies.txt';
$temporary_cookie_file_handler = fopen($temporary_cookie_file, 'w');
fclose($temporary_cookie_file_handler);
$user_data = array('TCNK'=>'authenticationEntryComponent', 'submitEvent'=>'1', 'guestLoginEvent'=>'', 'enterClicked'=>'true', 'bscf'=>'', 'bscv'=>'', 'targetEntid'=>'', 'ajaxSupported'=>'yes', 'screenName'=>$user_general_information['Username'], 'kclq'=>$user_general_information['Password']);
curl_setopt($process, CURLOPT_URL, 'https://www.edline.net/post/InterstitialLogin.page');
curl_setopt($process, CURLOPT_USERAGENT, 'Chrome/12.0.742.122');
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($user_data));
curl_setopt($process, CURLOPT_TIMEOUT, 0);
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($process, CURLOPT_COOKIEJAR, realpath($temporary_cookie_file));
curl_setopt($process, CURLOPT_COOKIEFILE, realpath($temporary_cookie_file));
curl_setopt($process, CURLOPT_HEADER, false);
$results_1 = curl_exec($process);
var_dump($results_1);
die();
I am trying to use POST form data to log into a site and then store the created cookies in a temporary file. I've looked at other discussions that talk about doing this and did as them but it doesn't work. When I try, it simply refreshes the page with no changes. I've been working on this for hours...Any ideas of what I am doing wrong?

Related

Trying to get json from get request http authenticated

I'm trying to get some data from an api that is protected by http authentication.
I'm want the response to be a json, but I'm getting a string innstead.
What can I do to fix this?
This is my code:
//init curl
$ch = curl_init();
$process = curl_init($url."?".$queryString);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json', ''));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, '');
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
print_r($return);

How to keep session informations using curl in php?

Access to fictitious page www.randomDomain.com/onlyForRegisteredUsers is possible only when I will log in first. So
I did this:
...
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->curl, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->currentUseragent;
curl_setopt($this->curl, CURLOPT_PROXY, $this->currentIP);
curl_setopt($this->curl, CURLOPT_URL, $this->currentUrl); //www.randomDomain.com/login/
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->currentData);
//with login RandomUserName and pass *******
$page = curl_exec($this->curl);
And successfully in $page I have got result something like this: ...<p>Welcome back, RandomUserName!</p>.
How to keep informations about session that I am already logged in, and then read content from www.randomDomain.com/onlyForRegisteredUsers ??
You can do this :
$cookieFile = 'cookie.txt';
curl_setopt($this->curl, CURLOPT_COOKIESESSION, true);
curl_setopt($this->curl, CURLOPT_COOKIEJAR, realpath($cookieFile));
curl_setopt($this->curl, CURLOPT_COOKIEFILE, realpath($cookieFile));
Note : the cookie file must exist, you can create the file if not exist with :
if (!file_exists(realpath($cookieFile)))
{
touch($cookieFile);
}

PHP CURL - Session expired

I'm trying to make an voip call with PHP CURL and MEGAVOIP.
The problem is i can't manage the session to access the page protected by a password.
I looked which variables are posted to the login page to post it with Curl.
But my code doesn't work.
Following Colin Morelli and Waygood's advices, I just added those lines in both commands:
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);
but it's still the same:
Megavoip returns: SESSION EXPIRED
So here is my full code:
<?php
ini_set("display_errors", 1);
$username="***";
$password="***";
$url="https://www.megavoip.com/login";
$url2="https://www.megavoip.com/phone_to_phone/";
$timeout = 10;
$cookies_file = 'cookies.txt';
// HERE I GET THE TOKEN
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
preg_match_all('/<input[^>]+>/i',$content, $result);
preg_match_all('/(id|value)=("[^"]*")/i',$result[0][5], $img);
$img1=str_replace('"', '', $img[0][0]);
$img2=str_replace('"', '', $img[0][1]);
$img1=substr($img1,3);
$img2=substr($img1,6);
$postdata = "login%5Busername%5D=".$username."&login%5Bpassword%5D=".$password."&page_referrer=login&".$img1."=".$img2;
// HERE I SEND THE VARIABLES
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$content = curl_exec($ch);
// IF LOGGED HERE I'LL MAKE THE CALL
curl_close($ch);
echo $content;
exit;
?>
Any ideas to help me?
This is a test account so feel free to use my login and password if you want to have a look on this and help me!
Thank you a lot in advance.
Ok you need to forward the cookie/session after login,
you need to first extract the cookie from Header after login like following
// HERE I GET THE TOKEN
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
.... ....
.... ....
$content = curl_exec($ch);
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $content, $m);
parse_str($m[1], $cookies);
$cookie = $cookies['NAMEOFCOOKIEUNEEDHERE'];
After that need to use $cookie variable in curl options like that
// HERE I SEND THE VARIABLES
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIE, 'NAMEOFCOOKIEUNEEDHERE='.$cookie);
I hope your problem will be resolved.
Thanks
Moin

I had a errors with Curl and SSL certificate

I had a problem. When you send a POST request with the CURL library to HTTPS get the error: SSL certificate problem, verify that the CA cert is OK. Details: error: 14090086: SSL routines: SSL3_GET_SERVER_CERTIFICATE: certificate verify failed. using current certificate.
I tried various certificates FROM http://www.startssl.com/certs/ and FROM http://curl.haxx.se/docs/caextract.html
Tell me what could be the cause of the error?
Here's the code POST request:
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($process, CURLOPT_ENCODING , '');
curl_setopt($process, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($process, CURLOPT_TIMEOUT, 120);
curl_setopt($process, CURLOPT_PROXY,$this->proxy);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process,CURLOPT_VERBOSE,1);
if($ssl){
curl_setopt ($process, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($process, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($process ,CURLOPT_CAINFO, YiiBase::getPathOfAlias('webroot').'/files/GTECyberTrustGlobalRoot.crt');
}
curl_setopt ($process, CURLOPT_HTTPHEADER, array('Expect:'));
$return = curl_exec($process);
$this->error_code = curl_getinfo($process, CURLINFO_HTTP_CODE);
Here is a working example. You should take a look a your options (reduce the number of option for test) and just set the CURLOPT_SSL_VERIFYPEER to false in order to disable the CA check.
// connect via SSL, but don't check cert
$handle=curl_init('https://www.google.com');
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$content = curl_exec($handle);
echo $content; // show target page
check HERE

Auto fill and submit forms on external site

I was wondering how does one auto fill multiple forms (using bot/local server) on multiple pages of external site (PHP) using ajax or curl.
For example a site www.abc.com/index.php has a form <form> <input name='text'></form> which takes you to the www.abc.com/fst.php when the form is submitted and there is another form on www.abc.com/fst.php that needs to be filled and submitted too. I want to automatically fill both form from my local server. How do i accomplish that?
The easiest way is to using something like greasemonkey ( https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/ ), but the better solution is to use the firebug 'net' tab to capture the post sent when you fill out the form and repeat that post with CURL ( http://php.net/manual/en/book.curl.php )
function post($url,$data) {
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($process, CURLOPT_ENCODING , $this->compression);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
if ($this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($process, CURLOPT_POST, 1);
$return = curl_exec($process);
curl_close($process);
return $return;
}

Categories