Wifi Authorization using Curl - php

session_start();
function sendAuthorization($id, $minutes)
{
$unifiServer = "https://xxx.xxx.xxx.xxx:xxxx";
$unifiUser = "xxxxx";
$unifiPass = "xxxxx";
// Start Curl for login
$ch = curl_init();
// We are posting data
curl_setopt($ch, CURLOPT_POST, TRUE);
// Set up cookies
$cookie_file = "/tmp/unifi_cookie";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
// Allow Self Signed Certs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// Force SSL3 only
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
// Login to the UniFi controller
curl_setopt($ch, CURLOPT_URL, "$unifiServer/login");
curl_setopt($ch, CURLOPT_POSTFIELDS,
"login=login&username=$unifiUser&password=$unifiPass");
// send login command
curl_exec ($ch);
// Send user to authorize and the time allowed
$data = json_encode(array(
'cmd'=>'authorize-guest',
'mac'=>$id,
'minutes'=> $minutes));
// Send the command to the API
curl_setopt($ch, CURLOPT_URL, $unifiServer.'/api/cmd/stamgr');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.$data);
curl_exec ($ch);
// Logout of the UniFi Controller
curl_setopt($ch, CURLOPT_URL, $unifiServer.'/logout');
curl_exec ($ch);
curl_close ($ch);
unset($ch);
}
(thought I would add the code first)
would anyone out there know how to turn this around so that I could call the function so the user IS NOT authorized?
Im making a captive portal. this is a authorized.php page, but on this i would like to have a function which is unauthorize within this page, to call it if some information is missing.
Thanks,
Andrew

Related

How to pass PHP_session_id in curl request

I am learning PHP and trying to automate a website login and then post some data to another page once logged in. I managed to login to the website and when I try to post data, I got a "Document moved".
Then I analysed the headers in firebug and realised that there was a PHP_session_id, so when I tried to manually pass this PHP_session_id it worked.
So my question is, how can I automatically get the sessionid when I login and then subsequently pass this on to my second request?
This is what my code looks like:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/login-main.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"loginType=company&username=johndoe%40gmail.com&password=1234&company=test");
ob_start();
curl_exec ($ch);
ob_end_clean();
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host:www.example.com',
'Origin:http://www.example.com',
'Cookie:PHPSESSID=na6ivnsdfab206ktb453o2au07',
'Referer:http://www.example.com/bookings/'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/bookings.php");
curl_setopt($ch, CURLOPT_POSTFIELDS,
"start_date=&end_date=&type=instructor&b_type=&id=41");
$buf2 = curl_exec ($ch);
curl_close ($ch);
echo "<PRE>".htmlentities($buf2);
?>
Add to your code in all curl requests
curl_setopt($ch, CURLOPT_COOKIEJAR, $full_path_to_cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $full_path_to_cookie_file);
For example
$full_path_to_cookie_file= __DIR__.'/cookie.txt';

getting json file behind login page with curl

I'm trying to get json data from this page https://www.rbauction.com/rba-api/search/results/advanced?rbasq=YXJ8Tj00Mjk0MjM4MzMyKzQyOTQ5NjU3OTg=&offset=2&count=1&ccb=USD
when i visit that page from browser it will ask me to login, when logged in it will display the json right away upon login. what need to do is get that json data with php using curl.
$username = 'myemail';
$password = 'mypass';
$loginurl = 'https://www.rbauction.com/myaccount';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginurl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '_58_login='.$username.'&_58_password='.$password);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $store = curl_exec($ch);
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_setopt($ch, CURLOPT_URL, 'https://www.rbauction.com/rba-api/search/results/advanced?rbasq=YXJ8Tj00Mjk0MjM4MzMyKzQyOTQ5NjU3OTg%3D&offset=2&count=1&ccb=USD');
$store = curl_exec($ch);
var_dump($store);
when i do var_dump its just showing empty string

Sending POST data to HTTPS redirect PHP

I need to redirect the current user to an external page (other domain) with some parameters that will be used on that page. To do that I'm using cURL, an example code is below:
$postfields = array('key'=>'value');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain.com/example.aspx');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);
curl_close ($ch);
But the user stays on the same page and I don't know if any data is transmitted.
What is the problem here? Is there another way to do that?

how to send post request by curl to web services in php

I need to fetch data from myallocator.com. I have his api address api.myallocator.com,
but when I send my data to them, it failed. I wrote some code for that which is shown below. Basically that pms property management system site.By that you can manage your property with his api.
$url = "http://api.myallocator.com/";
$xmlRequestString='<?xml version="1.0" encoding="UTF-8"?>
<GetProperties>
<Auth>
<UserId>"xxxxxxx"</UserId>
<UserPassword>"xxxxx"</UserPassword>
<VendorId>"xxxxxxx"</VendorId>
<VendorPassword>"xxxxxxxx"</VendorPassword>
</Auth>
</GetProperties>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequestString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$data = curl_exec($ch);
echo $data;
curl_close($ch);
Here Is the example of php curl with post try this
<?php
$ch = curl_init();
// set your site url
curl_setopt($ch, CURLOPT_URL,"http://testmysite.com/");
curl_setopt($ch, CURLOPT_POST, 1);
// postvar1, postvar2 .. are the parameters to send with post
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
if ($server_output == "OK") { ... } else { ... }
?>

yahoo.co.jp login using php curl?

I want to login yahoo.co.jp website using php and curl. i have already tried yahoo.com. its working very good. but when i am using the same format to login in yahoo.co.jp. But its not working. its going capcha verification.
I am trying many ways but its not working my code is given below.
<?php
set_time_limit(0);
//$php_userid = 'username';
//$php_password ='password';
Example login
$php_userid = 'jpnathanstock';
$php_password ='somepwpw!2345';
$cookie_file_path = "cookie.txt"; // Please set your Cookie File path
$fp = fopen($cookie_file_path,'wb');
fclose($fp);
$agent = "Mozilla/5.0 (Linux; U; Android 2.3.2; ja-jp; SonyEricssonSO-01C Build/3.0.D.2.79) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
$reffer = "http://login.yahoo.co.jp/config/login?.lg=jp&.intl=jp&logout=1&.src=www&.done=http://www.yahoo.co.jp";
// log out.
$LOGINURL = "http://us.ard.yahoo.com/SIG=12hoqklmn/M=289534.5473431.6553392.5333790/D=mail/S=150500014:HEADR/Y=YAHOO/EXP=1135053978/A=2378664/R=4/SIG=133erplvs/*http://login.yahoo.co.jp/config/login?logout=1&.done=http://auctions.yahoo.co.jp/&.src=ym&.lg=us&.intl=us";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
curl_close ($ch);
//1. Get first login page to parse hash_u,hash_challenge
$LOGINURL = "https://login.yahoo.co.jp/config/login?";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$loginpage_html = curl_exec ($ch);
curl_close ($ch);
preg_match_all("/name=\".u\" value=\"(.*?)\"/", $loginpage_html, $arr_hash_u);
preg_match_all("/name=\".challenge\" value=\"(.*?)\"/", $loginpage_html, $arr_hash_challenge);
$hash_u = $arr_hash_u[1][0];
$hash_challenge = $arr_hash_challenge[1][0];
//.tries=1&.src=auc&.last=http%3A%2F%2Flogin.yahoo.co.jp%2Fconfig%2Fedit_auc&promo=&.intl=jp&.bypass=&.partner=&.u=cq4ce2ov02mdi&.v=0&.fUpdate=Y&hasMsgr=0&.chkP=Y&.done=http%3A%2F%2Fuser.auctions.yahoo.co.jp%2Fjp%2Fshow%2Fmystatus&login=********&passwd=***********
// 2- Post Login Data to Page https://login.yahoo.com/config/login?
$LOGINURL = "https://login.yahoo.co.jp/config/login";
$POSTFIELDS = '.tries=1&.src=auc&.hash=&.last=http%3A%2F%2Flogin.yahoo.co.jp%2Fconfig%2Fedit_auc&promo=&.intl=jp&.bypass=&.partner=&.u='.$hash_u.'&.v=0&.fUpdate=Y&.challenge='.$hash_challenge.'&.hasMsgr=0&.chkP=Y&.done=http%3A%2F%2Fuser.auctions.yahoo.co.jp%2Fjp%2Fshow%2Fmystatus&login='.$php_userid.'&passwd='.$php_password;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
curl_close ($ch);
print_r($result);
exit;
?>
Please guide me. What's wrong my code. Advice me anyone..
You have a lingering http://us.ard.yahoo.com in the first $loginurl, it should probably be a *.co.jp, you need to check the docs to know the correct URL to use.
Another issue might be that you forgot to change the URL signature when you toggle between .co.jp and .com
EDIT
When i looked a bit more into your code, i noticed you immediately open then close the cookie jar, which won't work, you also open it with write binary wb flag, that's not needed. that's why you have an empty cookie.
what you need to do is
$cookie_file_path = "cookie.txt"; // Please set your Cookie File path
$fp = fopen($cookie_file_path,'w');
Then at the very end of your script you can close the cookie file
fclose($fp);
Finally , from my research it is not possible to bypass the yahoo login in oauth, as the login program is keep updating. And it is oauth, what you really need is
implement a link for the user to login
After login, store the access token and refresh token into your database
The access token is expire every 1 hour, so , before calling yahoo api you need to make sure access token not expire, otherwise, use refresh token to update your access token
So far, I test it for 4 - 5 days, it run without any problem, but I suspect the refresh token will expire, so in my program I warns the user to login again if the refresh token is expire
That's all. hope you can solve your problem

Categories