I want to create a script that can login using POST info, then browses through afew pages, on the same time. (After logging in) So I need to keep the session/cookie thing...
I've tried afew things, the logging in (POST) works fine.
But when I try to execute a second url, (the first being the login page) I lose the session/cookie and it the webpage says i'm no longer logged in:
Where am I going wrong with my code?
$url = 'http://www.example.com/authenticate.php';
$url2 = 'http://www.example.com/twourl.php';
$player = 'myuser';
$password = 'mypass';
$fields = array(
'player'=>urlencode($player),
'password'=>urlencode($password),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST,count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute post
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL,$url2);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute two url
curl_exec($ch);
//close connection
curl_close($ch);
Why is it not keeping me logged in on the second url?
Check out http://www.php.net/manual/en/book.curl.php#97160 I don't see where you are trying to pass the session to the site.
EDIT: you can also check out http://www.phpbuilder.com/board/showthread.php?t=10346748
Related
[Edited for better explanation and code included]
Hi! I have a php script on my web server that logs in to my heat pump web interface nibeuplink.com and gets all my temperature readings and so forth and returns them in a json-format.
freeboard.io is a free service for visualizing data, so I'm making a freeboard.io for my heat pump values. in freeboard.io I can add any json data as a data source, so I have added the link to my php-script. It fetches the data once but it seems there is some kind of cached values that it uses after that so they are not updated with new values from the script. freeboard.io uses a get-function to get the url. If i use a normal web browser to run the php script and refresh it, the values are updated - and also immediately updated in freeboard.io. Freeboard.io has a setting to automatically update the data source every 5 seconds.
It seems that there is something that triggers the script correctly when it is fetched from my web browser, but not when it is fetched from freeboard.io that uses a get function every 5 seconds to get new data.
in freeboard I can add headers to the get request, is there some header that would help me here to discard any cached data?
I hope that explains my problem better.
Is there anything i can add to my code in the beginning to always force an override of any cached data?
<?php
/*
* read nibe heatpump values from nibeuplink status web page and return them in json format.
* based on: https://www.symcon.de/forum/threads/25663-Heizung-Nibe-F750-Nibe-Uplink-auslesen-auswerten
* to get the code which is required as parameter, log into nibe uplink, open status page of your heatpump, and check url:
* https://www.nibeuplink.com/System/<code>/Status/Overview
*
* usage: nibe.php?email=<email>&password=<password>&code=<code>
*/
// to add additional debug output to the resulting page:
$debug = false;
date_default_timezone_set('Europe/Helsinki');
$date = time();
// Create temp file to store cookies
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
// URL to login page
$url = "https://www.nibeuplink.com/LogIn";
// Get Login page and its cookies and save cookies in the temp file
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); // Stores cookies in the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
// Now you have the cookie, you can POST login values
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Email=".$_GET['email']."&Password=".$_GET['password']);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); // Uses cookies from the temp file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Tells cURL to follow redirects
$output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://www.nibeuplink.com/System/".$_GET['code']."/Status/ServiceInfo");
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_POST, 0);
$result = curl_exec($ch);
$pattern = '/<h3>(.*?)<\/h3>\s*<table[^>]*>.+?<tbody>(.+?)<\/tbody>\s*<\/table>/s';
if ($debug) echo "pattern: <xmp>".$pattern."</xmp><br>";
$pattern2 = '/<tr>\s*<td>(.+?)<span[^>]*>[^<]*<\/span>\s*<\/td>\s*<td>\s*<span[^>]*>([^<]*)<\/span>\s*<\/td>\s*<\/tr>/s';
if ($debug) echo "pattern2: <xmp>".$pattern2."</xmp><br>";
preg_match_all($pattern, $result, $matches);
// build json format from matches
echo '{';
$first = true;
foreach ($matches[1] as $i => $title) {
echo ($first ? '"' : ',"').trim($title).'":{';
$content = $matches[2][$i];
preg_match_all($pattern2, $content, $values);
$nestedFirst = true;
foreach ($values[1] as $j => $field) {
echo ($nestedFirst ? '"' : ',"').trim($field).'":"'.$values[2][$j].'"';
$nestedFirst = false;
}
echo "}";
$first = false;
}
echo ",\"time\":{\"Last fetch\":\"$date\"}";
echo "}";
if ($debug) {
echo "<pre><xmp>";
echo print_r($matches);
echo "<br><br>";
echo $result;
echo "</xmp></pre>";
}
?>
You can make an ajax call to php script to refresh the part of webpage. I don't understand what do you mean by io i.e. are you talking about fetching the data from database and if any changes occurred in database then only newly added records must be fetched. If you mean it in that sense then you can use cookie to track any new records added into database and only if it finds new records it can make ajax call to php script to run your algorithm on fetched total dataset.
my problem is that i'm working with salesforce in wordpress, i'm not using wordpress-to-lead plugin, I have a form in a template and that form sends data to salesforce via cURL and also is posting data in database cause I have to generate a password and then send it to the user but its not working, is working salesforce but not saving data in the database, here is my code to post data in database and generate the password
$keysString = implode(", ", array_keys($blank_section));
unset($_POST['userId']);
$user_id = mysql_query("SELECT MAX(id) AS id FROM int_form_data");
$user_id = $user_id+11;
$passwordSend = 'INTELIGOS'.rand(10000, 5000000);
$array_user_id = array('user_id' => $user_id, 'password' => $passwordSend);
$posted_data = array_merge($_POST,$array_user_id);
foreach($posted_data as $k=>$v) {
$itfdatainfo[$k] = $v;
}
$itfkeys = array_keys($itfdatainfo);
$itfvalues = array_values($itfdatainfo);
if(isset($_POST['submit'])) {
$sql = "INSERT INTO int_form_data (".implode(',',$itfkeys).") VALUES('".implode("','",$itfvalues)."')";
$result = mysql_query($sql);
}
And here I use cURL to send data to Salesforce:
//Initialize the $query_string variable for later use
$query_string = "";
$kv = array();
//If there are POST variables
if ($_POST) {
//Initialize the $kv array for later use
//For each POST variable as $name_of_input_field => $value_of_input_field
foreach ($_POST as $llav => $value) {
//Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($llav)."=".stripslashes($value);
}
//Create a query string with join function separted by &
$query_string = join("&", $kv);
}
//Check to see if cURL is installed ...
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
//The original form action URL from Step 2
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
//Open cURL connection
$ch = curl_init();
//Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
//Set some settings that make it all work
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Execute SalesForce web to lead PHP cURL
$result = curl_exec($ch);
//close cURL connection
curl_close($ch);
Anyone knows why is happening that? I have all the code in one template in wordpress
I'm looking at this INSERT INTO int_form_data (".implode(',',$itfkeys).") and thinking if some column name in $itfkeys needs to be enclosed in backticks then it would cause an sql error.
I was wondering is it possible to automatically login to moodle after loging-in in aonther site? I tried to use curl but it seems not really working.
This is my testing code:
//set POST variables
$url = "http://moodlesite.com/login/index.php";
$fields = array(
'username' => 'username',
'password' => 'password'
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
I notice that after moodle login to the system, they redirect it again to http://moodlesite.com/login/index.php?testsession=userid and they need cookies it this case.
you can create a customized page for login into moodle.
code for that page .
<?php
require('config.php');
$name=$_REQUEST['name'];
$password=$_REQUEST['password'];
$dashboard = $CFG->wwwroot;
$user = authenticate_user_login($name, $password);
if(complete_user_login($user))
{
echo "login";
}
else
{
echo "not login";
}
?>
After creating that page you can pass name and password for login .
Not like that, Moodle needs to setup a session in your browser in order for you to be logged in. In general your requirement sounds strange - what exactly are you trying to achieve?
If you'd prefer to avoid your users logging in twice, you would be better implementing some sort of single sign on system:
Moodle supports CAS as standard ( http://docs.moodle.org/25/en/CAS_server_(SSO) )
Other add-on authentication plugins such as SAML and OAuth are available.
If none of those suit, you can also create your own authentication plugin. See Authentication plugins in Moodle docs and lib/authlib.php for details.
I want to process payments through my website and wanted to use sagepay for that purpose. I have gone through the guideline for sagepay go direct integration mentioned here:
http://www.sagepay.com/sites/default/files/pdf/user_guides/sagepaydirectprotocolandintegrationguidelines_0.pdf
Now,
I have created a script made necessary changes needed for the fields to process http request. here is the code:
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://live.sagepay.com/gateway/service/vspdirect-register.vsp';
$txcode = 'prefix_' . time() . rand(0, 9999);
$fields = array(
'VPSProtocol'=>urlencode("2.23"),
'TxType'=>urlencode("PAYMENT"),
'Vendor'=>urlencode("myvendorname"),
'VendorTxCode'=>urlencode($txcode),
'Amount'=>urlencode("2.00"),
'Currency'=>urlencode("GBP"),
'Description'=>urlencode("payment for my site"),
'CardHolder'=>urlencode('DELTA'),
'CardNumber'=>urlencode(4929000000006),
'ExpiryDate'=>urlencode(1213),
'CV2'=>urlencode(123),
'CardType'=>urlencode('VISA'),
'BillingSurname'=>urlencode('surname'),
'BillingFirstnames'=>urlencode('name'),
'BillingAddress1'=>urlencode(' clifton'),
'BillingCity'=>urlencode('Bristol'),
'BillingPostCode'=>urlencode('BS82UE'),
'BillingCountry'=>urlencode('United Kingdom'),
'DeliverySurname'=>urlencode('surname'),
'DeliveryFirstnames'=>urlencode('name'),
'DeliveryAddress1'=>urlencode(' clifton'),
'DeliveryCity'=>urlencode('Bristol'),
'DeliveryPostCode'=>urlencode('bs82ue'),
'DeliveryCountry'=>urlencode('united kingdom'),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
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);
print_r($result);
echo "end";
//close connection
curl_close($ch);
?>
But I am not getting any response back.. what could be the problem? Its in wamp server and non https url.
This code works for me. I added a few CURL functions and removed one and that did the trick.
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://live.sagepay.com/gateway/service/vspdirect-register.vsp';
$txcode = 'prefix_' . time() . rand(0, 9999);
$fields = array(
'VPSProtocol'=>urlencode("2.23"),
'TxType'=>urlencode("PAYMENT"),
'Vendor'=>urlencode("myvendorname"),
'VendorTxCode'=>urlencode($txcode),
'Amount'=>urlencode("2.00"),
'Currency'=>urlencode("GBP"),
'Description'=>urlencode("payment for my site"),
'CardHolder'=>urlencode('DELTA'),
'CardNumber'=>urlencode(4111111111111111),
'ExpiryDate'=>urlencode(1213),
'CV2'=>urlencode(123),
'CardType'=>urlencode('VISA'),
'BillingSurname'=>urlencode('surname'),
'BillingFirstnames'=>urlencode('name'),
'BillingAddress1'=>urlencode(' clifton'),
'BillingCity'=>urlencode('Bristol'),
'BillingPostCode'=>urlencode('BS82UE'),
'BillingCountry'=>urlencode('United Kingdom'),
'DeliverySurname'=>urlencode('surname'),
'DeliveryFirstnames'=>urlencode('name'),
'DeliveryAddress1'=>urlencode(' clifton'),
'DeliveryCity'=>urlencode('Bristol'),
'DeliveryPostCode'=>urlencode('bs82ue'),
'DeliveryCountry'=>urlencode('united kingdom'),
);
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute post
$result = curl_exec($ch);
print_r($result);
echo "end";
//close connection
curl_close($ch);
?>
FYI, using http_build_query() is great for building query_strings.
$fields_string = http_build_query($fields);
This code will work if you add field
Crypt='Your encryption password in your account setting in sagepay test';
But it returns null string if you use curl to post and it will be redirected to
https://test.sagepay.com/gateway/service/cardselection?vpstxid={BDBF098F-9BB5-3822-8CAE-AC0645A2CC57}
if you use Http POST.
Goodluck
Instead of printing the userinfo, the following code is printing the details of the access token granted by Google. The code is very simple yet I am not able to figure out the exact cause of this
<?php
$url = 'https://accounts.google.com/o/oauth2/token';
$fields = array(
'code'=>urlencode($_GET['code']),
'client_id'=>urlencode('###########.apps.googleusercontent.com'),
'client_secret'=>urlencode('###########'),
'redirect_uri'=>urlencode('http://######odie.co.in/googlogin'),
'grant_type'=>urlencode('authorization_code'),
);
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
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);
$data = json_decode($result);
print_r(file_get_contents(' https://www.googleapis.com/oauth2/v1/userinfo?access_token='.$data['access_token']));
?>
You need to add CURLOPT_RETURNTRANSFER, true to your curl_setopt.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
refer this. its very simple.
Simple PHP Library for Google Authentication API